title: PHP: creates file but can't append with (apparently) correct server permissions (LAMP)
body: <p>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.</p>
  
  <p>I am migrating an application from local XAMPP onto LAMP: hence problem only showing up now due to Windows/XAMPP giving no permission troubles.</p>
  
  <p>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 &amp; 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</p>
  
  <p>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.</p>
  
  <p>When I check the file involved it has following permissions:</p>
  
  <pre><code>-rw-r--r-- 1 daemon daemon 962 Oct  3 10:14 20151003logfile03-10-33530.txt
  </code></pre>
  
  <p>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"</p>
  
  <p>EDIT: adding directory info:
  Folder level permissions give my ftps user and daemon (group) full rwx access:</p>
  
  <pre><code>drwxrwx--- 2 ftpuser daemon 4096 Oct  3 10:30 logfiles
  </code></pre>
  
  <p>BUT it can't then fopen with "a"</p>
  
  <p>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</p>
  
  <p>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?  </p>
  
  <p>Any ideas?</p>
  
  <hr>
  
  <p>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.</p>
  
  <p>So what am I missing here? Why else might it be denying permission?</p>