简体   繁体   English

PHP move_uploaded_file“无法移动”

[英]PHP move_uploaded_file “Unable to move”

Apologies if this has been answered elsewhere and I didn't make the correlation to my failures in posting to move_upload_file() . 抱歉,如果其他地方都回答了这个问题,并且我没有将自己与发布到move_upload_file()失败相关联。 move_upload_file() is responding with 2 errors, "failed to open stream" and "unable to move". move_upload_file()响应有2个错误,“无法打开流”和“无法移动”。 I'm trying to keep this as simple as possible for testing purposes only (since it doesn't work). 我正尝试将其尽可能简单,仅出于测试目的(因为它不起作用)。 This site is not exposed to the internet nor is used for production purposes, but rather my learning. 这个站点不是暴露于互联网,也不是用于生产目的,而是我的学习。 A preemptive thank you for your patience and helpful tips on my troubles. 先发制人的谢谢您的耐心配合和对我遇到的麻烦的提示。

HTML form post: HTML表单发布:

<!DOCTYPE html>
<html>
<body>

<form enctype="multipart/form-data" action="recv.php" method="POST">
  <!-- MAX_FILE_SIZE must precede the file input field -->
  <input type="hidden" name="MAX_FILE_SIZE" value="3000000" />
  <!-- Name of input element determines name in $_FILES array -->
  Send this file: <input name="userfile" type="file" />
  <input type="submit" value="Send File" />
</form>

</body>
</html>

recv.php: recv.php:

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);

$uploaddir = '/var/www/html/Pictures/';
//$uploaddir = 'Pictures/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo "File: $uploadfile<br>\n";

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
  echo "File is valid, and was successfully uploaded.\n";
}

echo 'print_r($_FILES):';
print_r($_FILES);

print "</pre>";

?>

The result: 结果:

File: /var/www/html/Pictures/test.jpg

Warning:  move_uploaded_file(/var/www/html/Pictures/test.jpg): failed to open stream: Permission denied in /var/www/html/recv.php on line 13

Warning:  move_uploaded_file(): Unable to move '/tmp/phpuA2sGA' to '/var/www/html/Pictures/test.jpg' in /var/www/html/recv.php on line 13

print_r($_FILES):
Array
(
    [userfile] => Array
        (
            [name] => test.jpg
            [type] => image/jpeg
            [tmp_name] => /tmp/phpuA2sGA
            [error] => 0
            [size] => 43434
        )

)

And the correlated security (or lack thereof): 以及相关的安全性(或缺乏安全性):

drwxrwxrwt. 8 root   root   4096 Apr 10 16:00 /tmp
drwxrwxrwt. 3 root   root     35 Apr 10 16:00 /var/tmp/
drwxrwxrwt. 2 apache apache    6 Apr 10 16:53 /var/www/html/Pictures/

Probably the most important note that I cannot correlate is that this originally did work at one time but I had erased the contents of the html folder. 我无法关联的最重要的注释可能是,此命令原本可以一次工作,但我已删除了html文件夹的内容。 I noticed a linked file in Pictures that pointed back to /var/www/html . 我注意到“图片”中的链接文件指向/var/www/html Carelessly I removed that link and subsequently the few files I had in /var/www/html for testing. 我不小心删除了该链接,随后删除了我在/var/www/html进行测试的几个文件。 I've rebuilt those couple files per the above specs but now receive the warnings from the attempted move. 我已经按照上述规格重建了这些文件,但是现在收到尝试移动的警告。 As the recv.php file shows, I've tried both the absolute path and the relative path. 如recv.php文件所示,我已经尝试了绝对路径和相对路径。 I realize a lot of this is considered against best practice, but I need to understand what is broken before addressing all the other concepts. 我意识到很多这样的做法都被认为与最佳实践背道而驰,但是在解决所有其他概念之前,我需要先了解有什么不完善的地方。 Getenforce is still "Enforcing", if that matters. 如果重要的话,Getenforce仍在“执行”。

It was an selinux permission issue. 这是一个selinux许可问题。 chcon -R -t httpd_sys_rw_content_t Pictures solved the problem. chcon -R -t httpd_sys_rw_content_t Pictures解决了该问题。

I realized that the 777 permissions were not the issue when printing the results of is_writable('Pictures') , which wound up false. 我意识到在打印is_writable('Pictures')的结果时,不是777权限的问题,该结果最终是错误的。 A real quick test after setenforce 0 proved that I needed to address selinux. setenforce 0之后的真正快速测试证明,我需要解决selinux。

I have similar error: 我有类似的错误:

[11-Mar-2019 05:26:17 Asia/Shanghai] PHP Warning:  move_uploaded_file(uploads/my_image.jpg): failed to open stream: Permission denied in /var/www/example.com/public_html/upload.php on line 38
[11-Mar-2019 05:26:17 Asia/Shanghai] PHP Warning:  move_uploaded_file(): Unable to move '/tmp/phpOeWOf4' to 'uploads/my_image.jpg' in /var/www/example.com/public_html/upload.php on line 38

In my case, I solved it by sudo chown www-data uploads , which uploads is the folder name of upload destination: 就我而言,我通过sudo chown www-data uploads解决了该问题,其中uploads是上传目标的文件夹名称:

$ ls -la /var/www/example.com/public_html/uploads -d
drwxr-xr-x 2 www-data root 4096 Mac  11 05:29 /var/www/example.com/public_html/uploads

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM