Joshua Moniz
Joshua Moniz

Reputation: 50

PHP file permission issues on live server

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

Upvotes: 1

Views: 1324

Answers (1)

Progrock
Progrock

Reputation: 7485

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.

Upvotes: 1

Related Questions