title: PHP: File not writable after changing permissions and ownership
body: <p>In a PHP script , am getting below error when checked if a file is writable or not.</p>
  
  <pre><code>fopen(test.txt): failed to open stream: Permission denied
  </code></pre>
  
  <p>Please see blow details:</p>
  
  <ol>
  <li><p>Apache service is running as user:apache, checked with:</p>
  
  <p><code>ps aux | egrep '(apache|httpd)'</code></p></li>
  </ol>
  
  <p>2.Ownership of the file to be written and php script is changed to apache.</p>
  
  <pre><code>chown apache:apache test.txt 
  chown apache:apache test.php
  </code></pre>
  
  <p>3.File permissions changed to:777</p>
  
  <pre><code>chmod 777 test.txt 
  chmod 777 test.php
  </code></pre>
  
  <p>4.Tried setting:
  Already tried solutions provided in:<a href="https://stackoverflow.com/questions/10376048/fopen-permission-denied-on-a-file-with-777-permissions">fopen Permission denied on a file with 777 permissions</a></p>
  
  <p>OS:Redhat Enterprise</p>
  
  <p>Code:</p>
  
  <pre><code>&lt;?php
  echo getmyuid().':'.getmygid(); 
  echo "&lt;br/&gt;";
  echo exec('whoami');
  echo "&lt;br/&gt;";
  $dst = 'test.txt';
  echo $dst, file_exists($dst) ? ' exists' : ' does not exist&lt;br/&gt;', "&lt;br/&gt;\n";
  echo $dst, is_readable($dst) ? ' is readable' : ' is NOT readable', "&lt;br/&gt;\n";
  echo $dst, is_writable($dst) ? ' is writable' : ' is NOT writable&lt;br/&gt;', "&lt;br/&gt;\n";
  $fh = fopen($dst, 'w');
  if ( !$fh ) {
      echo ' last error: ';
      var_dump(error_get_last());
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>33:33
  apache
  test.txt exists
  test.txt is readable
  test.txt is NOT writable
  
  last error: array(4) { ["type"]=&gt; int(2) ["message"]=&gt; string(57) "fopen(test.txt): failed to open stream: Permission denied" ["file"]=&gt; string(34) "/var/www/data/test2.php" ["line"]=&gt; int(10) } 
  </code></pre>