0

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

上传.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:)

4

1 回答 1

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);
}

编辑:代码格式。

于 2012-06-19T22:20:01.843 回答