1
votes

Recently, I moved a website to live and all the file related operations stopped working, for e.g., copy, imagecreatefromjpeg, etc due to permissions issue.

All the files are created in files/ directory. I gave it 755 permission recursively for directories and 644 permission to all the files. Still the PHP functions didn't work. It works only if I give 777 permissions (not even 775 works).

I checked the permissions for other live projects. They had 755 for directories and 644 for files, and still they seem to work fine without any permission issues.

Could anyone please explain me the reason for this issue on this specific website?

Thanks

1
For the folders which you are trying to write (copy ,imagecreatefromjpeg) needs 777 permission explicitly .Otherwise it will not work(write permission issue) .Others folder you can set to 775 - Web Artisan
@BikashP But then how come they are working for other projects. I never had to set 777 permissions for those. - Joshua Moniz
Did you check the owning user and group of those files? Is the user under which the webserver runs the owner or member of the owning group? - Charlie Vieillard
Maybe in the other projects the owner of the folder was the same that execute the php process... generally www-data - Eloy Fernández Franco
Try changing the ownership to that user even though its the same name. - Charlie Vieillard

1 Answers

1
votes

It sounds like an ownership issue.

To test, create a folder on the server with 777 perms (e.g. tmp, perhaps in your document root).

And then create a script (alongside tmp) that does a simple write to that folder.

<?php

file_put_contents(__DIR__ . '/tmp/test.txt', 'hello earth');

Then look at the ownership and permissions of the resultant file.

On my dev server I have Php running under the web server Apache (using mod php) on Linux (Debian). And scripts run as the user 'www-data'. So the key here is that the user www-data needs to be able to write to the folder.

If the folder isn't writable by the web server, then something like the following error is in the apache error logs:

[Tue May 10 08:27:52.404959 2016] [:error] [pid 18] [client 172.17.42.1:59832] PHP Warning:  file_put_contents(/var/www/stackoverflow/tmp/test.txt): failed to open stream: Permission denied in /var/www/stackoverflow/so_test.php on line 3, referer: http://localhost/

Try and understand your server environments and unix permissions.