title: PHP Cronjob -- permission denied?
body: <p>So, I set up a cronjob to run the following command:</p>
  
  <p><code>php /var/www/path/to/cron/do-stuff.php</code></p>
  
  <p>The path is correct. The directory is owned by <code>www-data</code>. </p>
  
  <p>The script does the following:</p>
  
  <pre><code>chdir( getcwd() );
  echo shell_exec('whoami');
  
  $handle = fopen(uniqid('TEST_') . '.tst');
  fwrite($handle, 'foo');
  fclose($handle);
  </code></pre>
  
  <p>This is the output I get when trying to run the cron job:</p>
  
  <pre><code>www-data
  PHP Warning:  fopen(TEST_4e15266d09fa2.tst): failed to open stream: Permission denied
  </code></pre>
  
  <p>Yet when doing this from shell with <code>su www-data</code> -- the script executes just fine and produces the test file without a hiccup.</p>
  
  <p>Any idea why this might be?</p>
  
  <p>EDIT: I guess the <code>su www-data</code> is probably unnecessary, because php will run however it runs, but I tried that just in case. The cron and script is executed as <code>www-data</code> though, as you can see from the output.</p>