move_uploaded_file提供“无法打开流:权限被拒绝”,即使是777

时间:2018-03-13 11:04:02

标签: php apache permissions upload file-permissions

我在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'移动到   第14行/var/www/html/upload.php中的“/var/www/html/images/test.jpg”

两个文件夹都具有相同的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.

2 个答案:

答案 0 :(得分:1)

请您分享以下结果:

ls -lh /var/www/html

另外,按照https://serverfault.com/a/802350/429496

分享apache用户

答案 1 :(得分:1)

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

sudo chcon -t httpd_sys_rw_content_t /var/www/html/images -R
相关问题