title: PHP and file write permissions
body: <p>I have a folder with 3 different php scripts in it. email.php txt.php android.php</p>
  
  <p>I pipe emails and/or txts to their respective scripts, and use http POST to send data to the android script.</p>
  
  <p>Originally I only had the email and txt, and using this is was all ok</p>
  
  <pre><code>$data = '//data fields condensed into a single string obtained from email or txt';
  $filename= "output.php";
  $newFile= fopen($filename, 'w+');
  fwrite($newFile, $data);
  fclose($newFile);`
  </code></pre>
  
  <p>No problems, the scripts could open/write to the file, and if it didn't exits they would create it. the folder was not writeable.</p>
  
  <p>Now I have the android.php script and originally it could NOT open/create the output.php until I made the whole folder writeable. Now it can create output.php if need be.</p>
  
  <p>The problem now, is that either scripts can create/write to output.php HOWEVER, once the file is created by one of the scripts, the other ones cannot write to it
  Ie if the android script creates output.php, the email and txt scripts return errors
  if the email or txt scripts create output.php then the android script return error</p>
  
  <p>the error being 'unable to stream fopen' or something along those lines.</p>
  
  <p>Lorenzo (user here on SO) started to mention something about the users (ie the email being piped would be seen as a different user to an http POST command). The scripts create output.php with permissions 644 (no execute and only owner can write) and I am unable to manually change the permissions to 777 - although I would prefer to get a way that allows the scripts to create it - so I can empty the folder periodically for backup reasons and not have to remember to put it back in etc...</p>
  
  <p>I am thinking I could merge the 3 scripts into one (hopefully) but would prefer not too. Does anyone have any other ideas?</p>
  
  <p>Thanks</p>
  
  <p>Update: Since I am a new user and couldn't 'answer' my own question -</p>
  
  <p>Ok, so with the help of everyone who responded, I have worked out a solution:
  The email and txt scripts are run by user 'owner' and the htmlPOST is run by user '?'</p>
  
  <p>I had to make the folder chmod 777 in order for user '?' to work</p>
  
  <p>When each script runs, it checks for the file 'output.php'. If it didn't exist, then after the fclose I added a chmod 777 - that way the scripts run by other users could open/write it later. If the file already existed, then I didn't add the chmod because if it was the wrong user it created an error. 
  So a simple example of it:</p>
  
  <pre><code>$data = '//data fields condensed into a single string obtained from email or txt';
  $filename= "output.php";
  
  if (file_exists($filename)){
  $newFile= fopen($filename, 'w+');
  fwrite($newFile, $data);
  fclose($newFile);
  } else {
  $newFile= fopen($filename, 'w+');
  fwrite($newFile, $data);
  fclose($newFile);
  chmod($file, 0777);
  }
  </code></pre>
  
  <p>Thanks for your help!</p>