PHP upload an image file through url [closed]

吃可爱长大的小学妹 提交于 2019-12-22 01:23:12

问题


I wanna upload an image by url like

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

But I want an array like

array(
   'name' => '',
   'type' => '',
   'tmp_name' => '',
   'error' => (int) 4,
   'size' => (int) 0
),

Please suggest me how to get that?.


回答1:


Perhaps you need http://php.net/manual/en/function.file-get-contents.php and http://php.net/manual/en/function.file-put-contents.php

Combined together you can try;

// 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"];
  }
?>


来源:https://stackoverflow.com/questions/13743780/php-upload-an-image-file-through-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!