【发布时间】:2016-07-25 12:06:06
【问题描述】:
我无法使 php 上传工作。 PHP 5.4.45 Centos 6.7。 阿帕奇 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 目录,但无法移动到其他位置。 据我所知,这是一个权限问题。但是文件夹“文件”的权限似乎很好 - 777(所有者迈克尔=用户名):
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
我很乐意得到一些帮助或至少是寻求解决问题的方向。 谢谢。
【问题讨论】:
-
P.S.在我搬家之前,相同的代码正在另一台服务器上运行。
-
forum文件夹的权限呢? -
检查php中是否有任何警告。像这里一样激活所有日志:stackoverflow.com/questions/1053424/… 如果您没有任何警告表明第一个参数无效。在这里查看:php.net/manual/en/function.move-uploaded-file.php
-
它在该目录中创建了一个空文件这一事实让我认为这与权限无关,分区上是否有可用空间?尝试将另一个文件复制到 /home/michael/public_html/forum/files/ 作为网络用户(无论 PHP 以何种身份运行),看看是否有任何错误。
-
两个常见问题:首先,网络服务器没有以用户 michael 身份运行。它以 apache 或 www-user 或类似的方式运行。二、这是一个selinux盒子吗?
标签: php linux file-upload file-permissions permission-denied