Skip to content

PHP: creates file but can’t append with (apparently) correct server permissions (LAMP)

I have seen many questions and answers on this topic but none seem to help my situation. My PHP code is successfully creating a new logfile, but then cannot access that file to append further info, close it, etc.

I am migrating an application from local XAMPP onto LAMP: hence problem only showing up now due to Windows/XAMPP giving no permission troubles.

I started with a default Bitnami LAMP stack, and then manually setup relevant directory permissions on server: – my sftp user has rwx on htdocs and assorted out-of-web-root directories – apache is running as ‘daemon’ so I have given read & execute permissions to relevant directories for ‘daemon’ as group – in most directories I have disallowed write permissions for ‘daemon’ – however for my (application generated, internal) logs I have a ‘logfiles’ directory which has rwx for both my user and the ‘daemon’ group – ‘other’ is -rwx for all

When I run my application it falls over pretty much immediately. The error logs showing fopen failed to open stream: permission denied. However, the permissions indicate that it should have access.

When I check the file involved it has following permissions:

-rw-r--r-- 1 daemon daemon 962 Oct  3 10:14 20151003logfile03-10-33530.txt

This tells me that the file was created by Apache (i.e. by my PHP script) and that it has read and write permissions, from when I fopen() with “w”

EDIT: adding directory info: Folder level permissions give my ftps user and daemon (group) full rwx access:

drwxrwx--- 2 ftpuser daemon 4096 Oct  3 10:30 logfiles

BUT it can’t then fopen with “a”

I am assuming that this IS a file permission problem because: a) it works fine on XAMPP b) it states permission error in the error log

However, I can’t see why it should be a problem, given directly-specified OS-level permissions … maybe Apache requires an .htaccess ‘allow’ on this directory also?

Any ideas?

Clarification re why I don’t think CHMOD is the answer (sorry @RedAcid): CHMOD 777 etc is simply a way to set the underlying permissions I already have. Each digit represents 3 binary chars, so 7 is 111 (i.e. read, write and execute). As you can see above, I have read/write/execute for PHP/Apache on folder, together with read/write for file. What I’ve read suggests that you need execute at directory level, but not at file level because its not trying to execute the file.

So what am I missing here? Why else might it be denying permission?



OK – I found out the problem was higher level parent directory not having read/execute permissions. Now working! (AT LAST!)

For more detail see this previous question:

PHP fopen() fails on files even with wide-open permissions

User contributions licensed under: CC BY-SA
5 People found this is helpful