file_get_contents(): failed to open stream: Permission denied

16,179

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.

Share:
16,179
Admin
Author by

Admin

Updated on June 22, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm trying to load image from url. Everything works fine in localhost.

    When I run the code on my webserver it is saying

    Warning: file_get_contents(url): failed to open stream: Permission denied in /home/www/ .../imageup.php on line 64

    I've searched on google and tried with many answers from stackoverflow. Everyone is saying to set proper permission to the file and folder.

    I've set the permission for the php file and the folder to 777.

    Some answers saying to run some commands but I can't find the console to run that code so I allowed permission manually.

    I can understand that there is something with permission but can't figure out. (Someone may say this is an duplicate and may be it is, but I am fail to find solution)

    $file = file_get_contents($image['src']);