使用PHP脚本未将文件上载到服务器

时间:2014-10-01 23:49:56

标签: php file-upload amazon-ec2 webserver

所以我遵循一个简单的教程/示例,介绍如何使用HTML和PHP将文件上传到Web服务器。没有错误,一切似乎都运行正常,但文件没有保存在Web服务器上......

我正在使用Amazon EC2实例。 修改 我现在在echo输出中有权限警告/错误,如下所示,即使在目录上运行chmod 777之后

index.html

<form enctype="multipart/form-data" action="test.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

test.php的

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png","tiff");
$temp = explode(".", $_FILES["uploadedfile"]["name"]);
$extension = end($temp);
echo "Upload attempt...: " . $_FILES["uploadedfile"]["name"] . "<br>";
if ((($_FILES["uploadedfile"]["type"] == "image/gif")
     || ($_FILES["uploadedfile"]["type"] == "image/jpeg")
     || ($_FILES["uploadedfile"]["type"] == "image/tiff")
     || ($_FILES["uploadedfile"]["type"] == "image/jpg")
     || ($_FILES["uploadedfile"]["type"] == "image/pjpeg")
     || ($_FILES["uploadedfile"]["type"] == "image/x-png")
     || ($_FILES["uploadedfile"]["type"] == "image/png"))
    && ($_FILES["uploadedfile"]["size"] < 20000000)
    && in_array($extension, $allowedExts)) {

  if ($_FILES["uploadedfile"]["error"] > 0) {
    echo "Return Code: " . $_FILES["uploadedfile"]["error"] . "<br>";
  } else {
    echo "Upload: " . $_FILES["uploadedfile"]["name"] . "<br>";
    echo "Type: " . $_FILES["uploadedfile"]["type"] . "<br>";
    echo "Size: " . ($_FILES["uploadedfile"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["uploadedfile"]["tmp_name"] . "<br>";
    if (file_exists("~/upload/" . $_FILES["uploadedfile"]["name"])) {
      echo $_FILES["uploadedfile"]["name"] . " already exists. ";
    } else {
      move_uploaded_file($_FILES["uploadedfile"]["tmp_name"],
                         "../upload/" . $_FILES["uploadedfile"]["name"]);
      echo "Stored in: " . "../upload/" . $_FILES["uploadedfile"]["name"];
    }
  }
} else {
  echo "Invalid file";
}
?>

回声输出

Upload attempt...: storeflyer.jpg
Upload: storeflyer.jpg
Type: image/jpeg
Size: 35.1708984375 kB
Temp file: /tmp/phpqq6J4C
Warning: move_uploaded_file(../upload/storeflyer.jpg): failed to open stream: Permission denied in ../test.php on line 29 Warning: move_uploaded_file(): Unable to move '/tmp/phpqq6J4C' to '../upload/storeflyer.jpg' in ../test.php on line 29 Stored in: ../upload/storeflyer.jpg

1 个答案:

答案 0 :(得分:2)

您使用带有波浪号和斜杠的~/upload/作为路径。

您应该使用upload/作为相对路径删除所有的波浪号和斜线。

或者,正如sonic720在评论中所述:/var/www/upload这将因服务器而异,因此您的/var/users/www/public_html/upload可能是另一种选择。

如果从服务器的根目录执行脚本,请使用upload/

如果从子文件夹执行,您可能需要将其调整为,例如:../upload/

同时检查文件夹权限。 075507770755使用起来更安全。

  • 关于:__DIR__&amp; BASE_PATH

咨询:http://php.net/manual/en/function.dirname.php


修改

我注意到其他echo "Return Code: " . $_FILES["file"]["error"] . "<br>";应该是echo "Return Code: " . $_FILES["uploadedfile"]["error"] . "<br>";


error reporting添加到文件的顶部,这将有助于在生产测试期间。

error_reporting(E_ALL);
ini_set('display_errors', 1);