PHP - 文件上传失败

时间:2012-06-19 21:55:20

标签: php file upload permissions

我不明白为什么这不起作用。这应该很容易。

upload.php:

$uploaddir = '/usr/share/nginx/www/pitfax/upload/'; 
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo "<p>";

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {   
    echo "File is valid, and was successfully uploaded.\n"; 
} 
else {
    echo "Upload failed"; 
}

echo "</p>"; 
echo '<pre>'; 
echo 'Here is some more debugging info:'; 
print_r($_FILES); print "</pre>";

表单页面:

<form enctype="multipart/form-data" action="upload.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>

错误输出为:

Upload failed

Here is some more debugging info:Array (
    [userfile] => Array
        (
            [name] => test.pdf
            [type] => application/pdf
            [tmp_name] => /tmp/phptM0p4w
            [error] => 0
            [size] => 1287464
        )

)
  • 网络服务器:NginX
  • upload.php的权限:644
  • upload目录的权限:755
  • 使用root用户在SSH中工作。
  • 修改: upload_max_filesize = 2M
  • 修改:post_max_size = 8M

另一件事!上传完成后,我想获取上传文件的绝对路径。 (例如:/usr/share/nginx/www/pitfax/upload/test.pdf

1 个答案:

答案 0 :(得分:1)

如果$ uploadfile的值为 upload / test.pdf ,但您希望它 /usr/share/nginx/www/pitfax/upload/test.pdf 我怀疑这将是你的问题。

试试这段代码:

$uploadfile = '/usr/share/nginx/www/pitfax/upload/' . $_FILES['userfile']['name'];

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
  echo "File is valid, and was successfully uploaded.\n";
} else {
  print_r ($_FILES);
}

编辑:代码格式化。