我无法使 php 上传工作。 PHP 5.4.45 森托斯 6.7。 Apache 2.2.27。
我有一个 HTML 文件:
<form enctype="multipart/form-data" action="test2.php" method="POST">
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" name="submit"/>
</form>
我有 PHP 文件:
<?php
$uploaddir = '/home/michael/public_html/forum/files/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Error!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>
当我运行它时,我得到:
Error!
Here is some more debugging info:Array
(
[userfile] => Array
(
[name] => 184958.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpupab11
[error] => 0
[size] => 1473603
)
)
并且文件没有移动到新位置。相反,我在新位置获得了一个具有相同名称的 0 大小文件。
-rw-r--r-- 1 michael michael 0 Apr 5 16:33 184958.jpg
服务器日志记录错误:
[error] [client xx.xx.xx.xx] PHP Warning: move_uploaded_file():
Unable to move '/tmp/phpupab11' to '/home/michael/public_html/forum/files/184958.jpg'
in /home/michael/public_html/forum/test2.php on line 9
似乎文件已上传到/tmp 目录,但无法移动到其他位置。 据我所知,这是一个权限问题。但文件夹"file"的权限似乎不错 - 777(拥有者 michael=用户名):
drwxrwxrwx 2 michael michael 1216512 Apr 5 13:16 files
还有 PHP 变量:
post_max_size 20M
upload_max_filesize 20M
upload_tmp_dir /tmp
file_uploads On
memory_limit 1024M
我很乐意获得一些帮助或至少寻求解决问题的指导。 谢谢。
最佳答案
文件所有者必须是运行 Web 服务器的进程,可能是 apache。将 files 目录设置为具有写权限的 apache 所有。
关于PHP move_uploaded_file() 无故失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36428227/