简体   繁体   中英

Upload image file using href in move_uploaded_file( ) php

I need some advice and guide line. I am trying to upload image into a folder using link. And I am unable to upload an image. I am trying this code. This shows error message :

[function.move-uploaded-file]: failed to open stream: HTTP wrapper does not support writeable connections

<?php

  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {

    if (file_exists("http://localhost.myimage.com/uploads/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "http://localhost.myimage.com/uploads/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }

?>
<html>
<body>

<form action="" 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>

Is it possible ?

移动文件的目标需要是服务器的绝对路径,即

move_uploaded_file( $_FILES["file"]["tmp_name"], $_SERVER['DOCUMENT_ROOT'] ."/uploads/" . $_FILES["file"]["name"]);

You are supplying file_exists and move_uploaded_file with an URL. Try supplying a file on the hard drive instead. Change:

file_exists("http://localhost.myimage.com/uploads/" . $_FILES["file"]["name"])

to

file_exists($_SERVER['DOCUMENT_ROOT'] . "uploads/" . $_FILES["file"]["name"])

and similar to move_uploaded_file since that's where the error originated.

No it's not possible this way.

You try to store the uploaded file on a wrong destination.

I assume you own "http://localhost.myimage.com/uploads/" and wan't to store the file under '/uploads/' in your server.

If your project is installed eg under /var/www/my_prj/

And your upload is under /var/www/my_prj/uploads then your code should look like:

move_uploaded_file($_FILES["file"]["tmp_name"],
      "/uploads/" . $_FILES["file"]["name"]);

This only applies when '/var/www/my_prj/' is the DocumentRoot for your apache configured host.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM