PHP 警告:move_uploaded_file() 无法移动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13723174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Warning: move_uploaded_file() unable to move
提问by brendosthoughts
I've been slowly learning PHP and have found an array of information on the subject and solutions posted by other developers. I am attempting to have an android application upload a file to PHP server via HTTP post. However something is not working on my server side wile attempting to write to file in PHP.
我一直在慢慢学习 PHP,并找到了其他开发人员发布的有关该主题和解决方案的一系列信息。我正在尝试让一个 android 应用程序通过 HTTP post 将文件上传到 PHP 服务器。然而,我的服务器端试图用 PHP 写入文件时,有些东西不起作用。
Here is the PHP code:
这是PHP代码:
// Where the file is going to be placed
$target_path = "/var/www/media2net/uploads/uploads";
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename($_FILES['uploadedfile']['name']);
if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']) .
" has been uploaded";
chmod("uploads/" . basename($_FILES['uploadedfile']['name']), 755);
} else {
echo "There was an error uploading the file, please try again!";
echo "filename: " . basename( $_FILES['uploadedfile']['name']);
echo " target_path: " .$target_path;
}
I already know from inspecting wire shark on client side that http post is sent out correctly, also I have ensured that the directory I'm writing the file to has the correct permissions, and php safe mode is set to off.
我已经通过检查客户端的 Wireshark 知道正确发送了 http 帖子,我还确保我正在写入文件的目录具有正确的权限,并且 php 安全模式设置为关闭。
the output from apache2 error.log file reads
apache2 error.log 文件的输出读取
[Wed Dec 05 09:25:36 2012] [error] [client 74.14.162.250] PHP Warning:
move_uploaded_file(): Unable to move '/tmp/phpVLOnn3' to
'/var/www/media2net/uploads/downloaded_file.png'
in /var/www/media2net/upload.php on line 9
Any help with this problem or further ways to trouble shoot this would be appreciated.
对此问题的任何帮助或解决此问题的其他方法将不胜感激。
回答by Niclas Larsson
Change upload permissions for /var/www/media2net/uploads/ either by changing owner with "chown" or by "chmod"
通过使用“chown”或“chmod”更改所有者来更改 /var/www/media2net/uploads/ 的上传权限
Examples
例子
$ sudo chown apache:apache /var/www/media2net/uploads/
$ sudo chmod 755 /var/www/media2net/uploads/
Also, if downloaded_file.pngalready exists in that directory and it's owned by another user, then you would need to change ownership on that file as well.
此外,如果downloaded_file.png该目录中已经存在并且它由另一个用户拥有,那么您还需要更改该文件的所有权。
$ sudo chown apache:apache /var/www/media2net/uploads/downloaded_file.png
This way, it can be successfully overwritten by Apache.
这样,它就可以被 Apache 成功覆盖。
回答by Mona Jalal
This solved the problem for me:
这为我解决了问题:
$ sudo chown -R www-data:www-data /var/www/html/
回答by Emmanuel Mbuvi
This one worked well in Ubuntu Operating system. You only need to change the ownership
这个在 Ubuntu 操作系统中运行良好。您只需要更改所有权
sudo chown -R www-data:www-data (path to the image folder)
sudo chown -R www-data:www-data (path to the image folder)

