move_uploaded_file gives “failed to open stream: Permission denied” error

前端 未结 13 2007
攒了一身酷
攒了一身酷 2020-11-22 05:36

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

In php.ini:

upload_tmp_dir = /var/www/ht         


        
相关标签:
13条回答
  • 2020-11-22 05:51

    It happens if SELinux is enabled. Disable that in /etc/selinux/config by setting SELINUX=disabled and restart the server.

    0 讨论(0)
  • 2020-11-22 05:55

    This worked for me.

    sudo adduser <username> www-data
    sudo chown -R www-data:www-data /var/www
    sudo chmod -R g+rwX /var/www
    

    Then logout or reboot.

    If SELinux complains, try the following

    sudo semanage fcontext -a -t httpd_sys_rw_content_t '/var/www(/.*)?'
    sudo restorecon -Rv '/var/www(/.*)?'
    
    0 讨论(0)
  • 2020-11-22 05:57

    You can also run this script to find out the Apache process owner:

    <?php echo exec('whoami'); ?>

    And then change the owner of the destination directory to what you've got. Use the command:

    chown user destination_dir
    

    And then use the command

    chmod 755 destination_dir
    

    to change the destination directory permission.

    0 讨论(0)
  • 2020-11-22 06:00

    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. 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/
      
    3. 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/
      
    4. 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.

    0 讨论(0)
  • 2020-11-22 06:01

    Just change the permission of tmp_file_upload to 755 Following is the command chmod -R 755 tmp_file_upload

    0 讨论(0)
  • 2020-11-22 06:06

    Change permissions for this folder

    # chmod -R 0755 /var/www/html/mysite/images/

    0 讨论(0)
提交回复
热议问题