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

查看:38
本文介绍了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 中:

In 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):无法打开流:第 78 行/var/www/html/mysite/process.php 中的权限被拒绝

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(): 无法将 '/tmp/phpsKD2Qm' 移动到第 78 行/var/www/html/mysite/process.php 中的 '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/en/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天全站免登陆