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

前端 未结 13 2221
攒了一身酷
攒了一身酷 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 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.

提交回复
热议问题