简体   繁体   中英

PHP file upload not saving file

i want to write a PHP uploader, which saves files onto my FTP.

I found this script:

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_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 "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

I added a 0 to the max filesize value, because i wanted to test with an 160kb image.

Everything works fine. It even says:

Upload: bier15291.jpg
Type: image/jpeg
Size: 158.26171875 kB
Temp file: /mnt/shared/tmp/php_0aOep
Stored in: upload/bier15291.jpg

But i cant find the file on my FTP.

What am i doing wrong?

Please check the return value of move_uploaded_file. If either the filename is not a valid upload file or something goes wrong with moving the file contents, move_uploaded_file will return false.

Take a look at this: http://www.php.net/manual/en/features.file-upload.errors.php You need some error handling at the point of your attempt to move the file, as currently you only check IF $_FILES["file"]["error"] > 0 before you actually attempt to move the file.

So your code only confirms there are no errors on the temp file before you move it.

Consider these statements from http://www.php.net/manual/en/function.move-uploaded-file.php

If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE.

If filename is a valid upload file, but cannot be moved for some reason, no action will occur, and move_uploaded_file() will return FALSE. Additionally, a warning will be issued.

So you do right checking the temp file is all ok and no errors before working with it, but you also need to catch errors from the move_upload_file() function to make sure the move was TRUE or FALSE.

Do something like (I've just modified your code to add an IF around the move_uploaded_file() )

   if (file_exists("upload/" . $_FILES["file"]["name"]))
     {  
       echo $_FILES["file"]["name"] . " already exists. ";
     }
   else
     {
       if (move_uploaded_file($_FILES["file"]["tmp_name"],
       "upload/" . $_FILES["file"]["name"]))
         {
           echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
         }
       else
         {
           //output the errors here (ie using $_FILES["file"]["error"])
         }
      }

Then the error array should output what's going wrong. Ie (likely) UPLOAD_ERR_CANT_WRITE. This will then point you to the issue, and also is good to have in your code anyway, so your script fails gracefully rather than just nothing happens (should never have a scenario where nothing happens!)

所以解决方案是设置权限0775 ...

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