【问题标题】:PHP upload an image file through url [closed]PHP通过url上传图像文件[关闭]
【发布时间】:2012-11-24 11:48:06
【问题描述】:

我想通过网址上传图片

'C:\wamp\www\reelstubs\app\webroot\img\movies\0bf522bb76777ba43393b9be5552b263.jpg'

但我想要一个像

这样的数组
array(
   'name' => '',
   'type' => '',
   'tmp_name' => '',
   'error' => (int) 4,
   'size' => (int) 0
),

请建议我如何获得它?

【问题讨论】:

  • 需要更多细节,你试过什么?
  • @jakenoble 我没试过.. 建议我如何获得?
  • 您可能正在寻找PHP manual on POST method uploads
  • file:// 附加到路径的开头并使用parse_url 获取名称、类型等

标签: php image-upload


【解决方案1】:

也许你需要http://php.net/manual/en/function.file-get-contents.phphttp://php.net/manual/en/function.file-put-contents.php

结合起来可以试试;

// Your file
$file = 'C:\wamp\www\reelstubs\app\webroot\img\movies\0bf522bb76777ba43393b9be5552b263.jpg';

// Open the file to get existing content
$data = file_get_contents($file);

// New file
$new = 'C:\wamp\www\reelstubs\app\webroot\img\movies\newimage.jpg';

// Write the contents back to a new file
file_put_contents($new, $data);

【讨论】:

    【解决方案2】:
         The enctype attribute of the <form> tag specifies which content-type to use when 
        submitting the form.
        "multipart/form-data" is used when a form requires binary data, like the contents of a
         file, to be uploaded
         so please use the post method for uploading a file because post method can handle unlimited data. but get method not not handle more than 8mb data . 
            <html>
              <body>
                    <form action="upload_file.php" method="post"
                    enctype="multipart/form-data">
                    <label for="file">Filename:</label>
                    <input type="file" name="file" id="file"><br>
                    <input type="submit" name="submit" value="Submit">
                    </form>
    
                    </body>
                    </html>
        you can handle the array on upload_file.php page .
    upload_file.php
    
    <?php
    if ($_FILES["file"]["error"] > 0)
      {
      echo "Error: " . $_FILES["file"]["error"] . "<br>";
      }
    else
      {
      echo "Upload: " . $_FILES["file"]["name"] . "<br>";
      echo "Type: " . $_FILES["file"]["type"] . "<br>";
      echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
      echo "Stored in: " . $_FILES["file"]["tmp_name"];
      }
    ?>
    

    【讨论】:

    • 我知道你写了什么..但我想通过 url 上传文件..
    • 我们想像 imgur 一样使用 URL 上传文件。对@Sanganabasu? :)