1

我想编写一个 PHP 上传器,它将文件保存到我的 FTP 上。

我找到了这个脚本:

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

我在最大文件大小值中添加了 0,因为我想用 160kb 的图像进行测试。

一切正常。它甚至说:

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

但我无法在我的 FTP 上找到该文件。

我究竟做错了什么?

4

3 回答 3

1

请检查 move_uploaded_file 的返回值。如果文件名不是有效的上传文件或移动文件内容出现问题,move_uploaded_file 将返回 false。

于 2013-08-21T13:27:58.440 回答
0

看看这个: http ://www.php.net/manual/en/features.file-upload.errors.php 您需要在尝试移动文件时进行一些错误处理,因为目前您只检查IF $_FILES["file"]["error"] > 0在您实际尝试移动文件之前。

因此,您的代码仅在您移动临时文件之前确认临时文件没有错误。

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!)

于 2013-08-21T13:55:24.517 回答
0

So the solution was to set the permissions 0775...

于 2013-08-22T17:25:42.787 回答