简体   繁体   English

没有使用PHP脚本将文件上传到服务器

[英]Files not being uploaded to server using PHP script

So Im following a simple tutorial/example on how to upload files to a web server using HTML and PHP. 因此,我遵循一个简单的教程/示例,该示例是关于如何使用HTML和PHP将文件上传到Web服务器的。 There are no errors and everything seems to work fine, but the file is not saved on the web server... 没有错误,一切似乎都可以正常工作,但是文件未保存在Web服务器上...

I'm using an Amazon EC2 instance. 我正在使用Amazon EC2实例。 EDIT: I now have permission warnings/errors in my echo output as shown below, even after running chmod 777 on the directories 编辑:现在,即使在目录上运行chmod 777,我的回声输出中也有权限警告/错误,如下所示

index.html 的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 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";
}
?>

echo output 回声输出

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

You're using ~/upload/ with a tilde and slash as path. 您正在使用~/upload/并使用波浪号和斜杠作为路径。

You should remove all of the tildes and slashes by using upload/ as a relative path. 您应该通过使用upload/作为相对路径来删除所有波浪号和斜杠。

Or, as stated by sonic720 in a comment: /var/www/upload this will differ from server to server, so yours may be something like /var/users/www/public_html/upload that is another option. 或者,如sonic720在评论中所述: /var/www/upload这在服务器之间是不同的,因此您的名称可能类似于/var/users/www/public_html/upload

Use upload/ if executing your script from the root of your server. 如果从服务器的根目录执行脚本,请使用upload/

If executed from a sub-folder, you may need to adjust it to, and for example: ../upload/ 如果从子文件夹执行,则可能需要对其进行调整,例如: ../upload/

Also check for folder permissions. 同时检查文件夹权限。 Either 0755 or 0777 , yet 0755 is safer to use. 07550777还是0755使用起来更安全。

  • About: __DIR__ & BASE_PATH 关于: __DIR__BASE_PATH

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


Edit: 编辑:

I noticed something else echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; 我注意到其他echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; that should read as echo "Return Code: " . $_FILES["uploadedfile"]["error"] . "<br>"; 应该显示为echo "Return Code: " . $_FILES["uploadedfile"]["error"] . "<br>"; echo "Return Code: " . $_FILES["uploadedfile"]["error"] . "<br>";


Add error reporting to the top of your file(s) which will help during production testing. 错误报告添加到文件顶部,这将在生产测试期间提供帮助。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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