Search code examples
phploggingpermissionsfile-permissionsfopen

Why the PHP fopen() function is not working with 755 and 775 folder permissions?


I am using ACRA (https://github.com/ACRA/acra) for automatically posting my Android crash reports to a report server. I have a PHP script that uses the following lines for writing the log file to the https://example.com/logs/ folder on my testing server:

$FileLog = $_SERVER['DOCUMENT_ROOT'] . "/logs/" . $fname;
$HandleLog = fopen($FileLog, 'a');

The PHP fopen() function is only working correctly for me when the logs/ folder on my server has these permissions:

drwxrwxrwx (777)

I already tried drwxrwxr-x (775) and drwxr-xr-x (755) and the fopen() function is not working. It only works when I use drwxrwxrwx (777) for the logs/ folder permissions.

That is weird to me because the PHP script that I am running is on my server, so when I execute fopen($FileLog, 'a') from that PHP file on my server, it should work correctly if https://example.com/logs/ is a folder with drwxrwxr-x (775) permissions, since the PHP script that uses fopen($FileLog, 'a') is also on the same https://example.com server. Do you have any hints about why only 777 permissions are working? I know drwxrwxrwx (777) permissions for a web server is a no-no. Thanks.

UPDATE 1:

The PHP script that uses fopen($FileLog, 'a') is hosted at https://example.com/loggingscript.php and it is invoked by the ACRA library (https://github.com/ACRA/acra) from my Android app when it crashes.


Solution

  • The directory should be owned by the user that runs the webserver. Otherwise, it needs world-write permission so that other users can write to it.

    This might be something like www-data or apache. If you're not sure what this is, see

    Finding out what user Apache is running as?