PHP move_uploaded_file()无故失败 [英] PHP move_uploaded_file() FAILS without reason
问题描述
我无法使php上传工作. PHP 5.4.45 Centos 6.7. Apache 2.2.27.
I can't make php upload work. PHP 5.4.45 Centos 6.7. Apache 2.2.27.
我有一个HTML文件:
I have a HTML file:
<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>";
?>
当我运行它时,我得到:
When I run it I get:
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的文件,名称相同.
And the file is not moved to new location. Instead I get a 0-size file with same name in new location.
-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 dir,但无法移动到其他位置. 就我所知,这是一个权限问题.但是文件夹文件"的许可似乎很好-777(所有者为michael = username):
Seems the file is uploaded good to /tmp dir, but cannot be moved to another location. As much as I read it is a permission problem. But permission of folder "files" is seems good - 777 (with owner michael=username):
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
我很乐意寻求帮助或至少提供指导以寻求解决问题的方法. 谢谢.
I would glad to get some help or at least direction to look for to resolve the issue. Thank you.
推荐答案
问题尚未解决. /home下的任何文件夹都无法接收上传的文件.权限/所有权更改均无济于事. 无论如何,找到了解决方法.这并不理想,也不安全,但是可以正常工作,直到我找出导致这种奇怪行为的原因为止,这已经足够了. 我在根目录(/files_of_forum)和权限(michael:michael 777)中创建了一个文件夹,并在论坛中将附件的目录(原来的文件"更改为"../../../../files_of_forum".将所有文件从原始文件夹移至新文件夹. 现在一切正常. 感谢所有试图提供帮助的东西.
The problem was not solved. No folder under /home was able to receive uploaded files. Neither permissions/ownership changes helped. Anyway, found workaround. Not ideal and not secure, but works and that's good enough until I'll find out the reason for that strange behavior. I created a folder in root dir (/files_of_forum) and the permissions (michael:michael 777) and changed in forum the dir of the attachments (what was original "files" to "../../../../files_of_forum". Moved all files from original folder to the new one. Now everything works. Thank all what tried to help.
这篇关于PHP move_uploaded_file()无故失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!