move_uploaded_file给出“无法打开流:权限被拒绝".错误 [英] move_uploaded_file gives "failed to open stream: Permission denied" error

查看:528
本文介绍了move_uploaded_file给出“无法打开流:权限被拒绝".错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在CentOS上使用Apache 2.2和PHP 5.3配置上传目录时,我总是收到此错误.

I keep getting this error when trying to configure the upload directory with Apache 2.2 and PHP 5.3 on CentOS.

在php.ini中:

upload_tmp_dir = /var/www/html/mysite/tmp_file_upload/

在httpd.conf中:

In httpd.conf:

Directory /var/www/html/mysite/tmp_file_upload/>
    Options  -Indexes
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>
<Directory /var/www/html/mysite/images/>
                Options -Indexes
</Directory>

CentOS目录权限:

CentOS directory permissions:

drwxrwxr-x 2 root root 4096 Nov 11 10:01 images
drwxr-xr-x 2 root root 4096 Nov 12 04:54 tmp_file_upload

无论我做什么,上传文件时都会不断从PHP收到此错误:

No matter what I do, I keep getting this error from PHP when I upload the file:

警告:move_uploaded_file(images/robot.jpg):无法打开流:在/var/www/html/mysite/process.php的第78行,权限被拒绝

Warning: move_uploaded_file(images/robot.jpg): failed to open stream: Permission denied in /var/www/html/mysite/process.php on line 78

警告:move_uploaded_file():在第78行的/var/www/html/mysite/process.php中无法将"/tmp/phpsKD2Qm"移动到"images/robot.jpg"

Warning: move_uploaded_file(): Unable to move '/tmp/phpsKD2Qm' to 'images/robot.jpg' in /var/www/html/mysite/process.php on line 78

如您所见,它从未从php.ini文件中获取有关上传文件的配置.

As you can see, it never did take the configuration from the php.ini file regarding the upload file.

我在做什么错了?

推荐答案

这是因为imagestmp_file_upload仅可由root用户写入.为了使上传正常工作,我们需要使这些文件夹的所有者与httpd进程所有者相同,或者使其在全局范围内可写(错误做法).

This is because images and tmp_file_upload are only writable by root user. For upload to work we need to make the owner of those folders same as httpd process owner OR make them globally writable (bad practice).

  1. 检查apache进程所有者:$ps aux | grep httpd.第一列将是所有者,通常为nobody
  2. imagestmp_file_upload的所有者更改为nobody或在步骤1中找到的任何所有者.

  1. Check apache process owner: $ps aux | grep httpd. The first column will be the owner typically it will be nobody
  2. Change the owner of images and tmp_file_upload to be become nobody or whatever the owner you found in step 1.

$sudo chown nobody /var/www/html/mysite/images/

$sudo chown nobody /var/www/html/mysite/tmp_file_upload/

现在,如果需要,所有者可以写

  • Chmod imagestmp_file_upload [好像您已经准备好了这个].在@Dmitry Teplyakov答案中提及.

  • Chmod images and tmp_file_upload now to be writable by the owner, if needed [Seems you already have this in place]. Mentioned in @Dmitry Teplyakov answer.

    $ sudo chmod -R 0755 /var/www/html/mysite/images/
    
    $ sudo chmod -R 0755 /var/www/html/mysite/tmp_file_upload/
    

  • 有关为什么会发生此行为的更多详细信息,请查看手册 http://php.net/manual/zh-CN/ini.core.php#ini.upload-tmp-dir ,请注意,它也讨论了open_basedir指令.

  • For more details why this behavior happend, check the manual http://php.net/manual/en/ini.core.php#ini.upload-tmp-dir , note that it also talking about open_basedir directive.

    这篇关于move_uploaded_file给出“无法打开流:权限被拒绝".错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆