本文介绍了PHP 警告:move_uploaded_file() 无法移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在慢慢学习 PHP,并找到了其他开发人员发布的有关该主题和解决方案的一系列信息.我正在尝试让一个 android 应用程序通过 HTTP post 将文件上传到 PHP 服务器.但是,我的服务器端试图用 PHP 写入文件时,有些东西不起作用.

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代码:

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

我已经通过检查客户端的 Wireshark 知道正确发送了 http 帖子,并且我还确保我正在写入文件的目录具有正确的权限,并且 php 安全模式设置为关闭.

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.

apache2 error.log 文件的输出读取

the output from apache2 error.log file reads

[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.

推荐答案

通过使用chown"或chmod"更改所有者来更改/var/www/media2net/uploads/的上传权限

Change upload permissions for /var/www/media2net/uploads/ either by changing owner with "chown" or by "chmod"

示例

$ sudo chown apache:apache /var/www/media2net/uploads/
$ sudo chmod 755 /var/www/media2net/uploads/

此外,如果 downloaded_file.png 已经存在于该目录中并且它由另一个用户拥有,那么您也需要更改该文件的所有权.

Also, if downloaded_file.png already exists in that directory and it's owned by another user, then you would need to change ownership on that file as well.

$ sudo chown apache:apache /var/www/media2net/uploads/downloaded_file.png

这样就可以被Apache成功覆盖了.

This way, it can be successfully overwritten by Apache.

这篇关于PHP 警告:move_uploaded_file() 无法移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 14:59