Saturday, November 14, 2015

move_uploaded_file gives “failed to open stream: Permission denied ” error after all configurations i did


http://stackoverflow.com/questions/8103860/move-uploaded-file-gives-failed-to-open-stream-permission-denied-error-after

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/html/mysite/tmp_file_upload/
In httpd.conf:
Directory /var/www/html/mysite/tmp_file_upload/>
    Options  -Indexes
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>
<Directory /var/www/html/mysite/images/>
                Options -Indexes
</Directory>
CentOS directory permissions:
drwxrwxr-x 2 root root 4096 Nov 11 10:01 images
drwxr-xr-x 2 root root 4096 Nov 12 04:54 tmp_file_upload
No matter what I do, I keep getting this error from PHP when I upload the file:
Warning: move_uploaded_file(images/robot.jpg): failed to open stream: Permission denied in /var/www/html/mysite/process.php on line 78
Warning: move_uploaded_file(): Unable to move '/tmp/phpsKD2Qm' to 'images/robot.jpg' in /var/www/html/mysite/process.php on line 78
As you can see it never did take the configuration from the php.ini file regarding the upload file.
What am I doing wrong here?
shareimprove this question

    
775? Maybe your server is running as nobody. Only root can write in this case (your "images" permissions)... – xfix Nov 12 '11 at 10:34
    
what does it means ? how can i change it ? – user63898 Nov 12 '11 at 11:16

7 Answers

up vote 111 down vote accepted
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.