【问题标题】:move_uploaded_file gives “failed to open stream: Permission denied ” even with 777move_uploaded_file 给出“打开流失败:权限被拒绝”,即使是 777
【发布时间】:2018-08-21 14:42:27
【问题描述】:

我在 CentOS 上使用 Apache 2.4 和 PHP 7 时遇到此错误。

我有这样的文件夹结构:

var
  -- www
    -- html
      -- upload.php
      -- uploads
      -- images

当我运行 move_uploaded_file( $tmpDir, '/var/www/html/uploads' ); - 一切都完美无缺。

但是当我尝试将目标目录更改为 /var/www/html/images 时出现错误。

警告:move_uploaded_file(/var/www/html/images/test.jpg):无法打开流: 第 14 行 /var/www/html/upload.php 中的权限被拒绝

警告:move_uploaded_file():无法将“/tmp/phps3D2Em”移动到 '/var/www/html/images/test.jpg' 在 /var/www/html/upload.php 第 14 行

两个文件夹具有相同的 755 模式和相同的所有者 (apache)。但不知何故,当我想在图像文件夹中上传时,它给了我错误。

我从 PHP 运行了 posix_getuid(),它给了我 Apache 用户的 ID。

可能是什么问题?

编辑:

ls -lh /var/www/html 的结果:

-rw-r--r--.  1  root    root  2.6K Mar 10 13:02 upload.php
drwxr-xr-x.  5  apache  root    23 Mar 12 16:06 images
drwxr-xr-x.  2  apache  root   88K Mar 13 12:36 uploads

httpd -S 的结果:

ServerRoot: "/etc/httpd"
Main DocumentRoot: "/var/www/html"
Main errorLog: "/etc/httpd/logs/error_log"
Mutex authdigest-client: using_defaults
Mutex ssl-stapling: using_defaults
Mutex proxy: using_defaults
Mutex authn-socache: using_defaults
Mutex default: dir="/run/httpd" mechanism=default
Mutex mpm-accept: using_defaults
Mutex authdigest-opaque: using_defaults
Mutex proxy-balancer-shm: using_defaults
Mutex rewrite-map: using_defaults
PidFile: "/run/httpd/httpd.pid"
Define: _RH_HAS_HTTPPROTOCOLOPTIONS
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="apache" id=48
Group: name="apache" id=48

编辑 2: 我在 /var/www/html/perms.php 上设置了这个脚本:

print posix_getuid();
$dirs = array_filter(glob('*'), 'is_dir');
foreach( $dirs as $dir ) {
  if ( is_writable($dir) ) print "{$dir} is writable.<br>";
  else print "{$dir} is not writable.";
}

和输出:

48
images is not writable. Permissions may have to be adjusted.
uploads is writable.

【问题讨论】:

  • photos? images?是哪个?
  • 照片 != 图片
  • 抱歉,这是图片。我已经更新了问题。

标签: php apache permissions upload file-permissions


【解决方案1】:

我通过输入以下命令解决了这个问题:

sudo chcon -t httpd_sys_rw_content_t /var/www/html/images -R

【讨论】:

    【解决方案2】:

    能否分享一下结果:

    ls -lh /var/www/html

    另外,共享 apache 用户为:https://serverfault.com/a/802350/429496

    【讨论】:

    • 我已经更新了 ls -lh 的结果。并且 apachectl -S 对我不起作用,因为我安装了 systemctl。不知道哪个命令适合systemctl httpd。
    • 您必须安装 Apache 2。使用apache2ctl 而不是apachectl
    • apache2ctl 还是没有,我用 httpd -S 解决了,我用结果更新了问题。
    • 是的。你说的对。我刚刚看到你正在使用 CentOS。我不知何故认为你有 Ubuntu。可能我混合了两个不同的帖子。
    • 我为文件夹权限检查设置了一个自定义脚本,请参阅我的问题上的编辑 2。
    猜你喜欢
    • 2013-06-19
    • 2019-07-25
    • 2011-12-27
    • 1970-01-01
    • 2012-05-03
    相关资源
    最近更新 更多