简体   繁体   中英

move_uploaded_file gives “failed to open stream: Permission denied ” even with 777

I get this error with Apache 2.4 and PHP 7 on CentOS.

I have folder structure like this:

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

And when I run move_uploaded_file( $tmpDir, '/var/www/html/uploads' ); - everything works flawlessly.

But the error comes when I try to change the destination directory to /var/www/html/images.

Warning: move_uploaded_file(/var/www/html/images/test.jpg): failed to open stream: Permission denied in /var/www/html/upload.php on line 14

Warning: move_uploaded_file(): Unable to move '/tmp/phps3D2Em' to '/var/www/html/images/test.jpg' in /var/www/html/upload.php on line 14

Both folders have the same 755 mode and same owner (apache). But somehow it gives me error when I want to upload in images folder.

I ran posix_getuid() from PHP and it gives me ID of the Apache user.

What could be the issue?

Edit:

Results of 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

Results of 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

Edit 2: I have this script set on /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.";
}

and the output:

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

Could you please share the results of:

ls -lh /var/www/html

Also, share the apache user as per: https://serverfault.com/a/802350/429496

我通过键入以下命令解决了该问题:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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