rycharn
05-04-2009, 12:26 AM
I want my php script to update (append) a text file (log.txt) when the script executes. It runs fine when it is called from a browser, but not when cron runs it. Permission is set at 755. Here's a simplified version of my script:
#!/usr/local/bin/php
<?php
$newline=chr(10);
$now=time().$newline;
$handle=fopen("log.txt", "a+");
fwrite($handle, $now);
fclose($handle);
?>
I have also tried a version with "echo $now();" before the closing tag, which causes cron to send me an email with the output of the script. The email has the expected result, so I know there's nothing wrong with the variable, and I know the script is executing.
I've also tested the script with "file_exists" and "is_writable" variations; the script sees the file, and considers it writable. If I substitute "$result=fwrite($handle, $now);" I get a result. The script clearly thinks it is updating log.txt, but when I check that file, it is unchanged.
Again, when I execute the same script from a browser, it works fine -- the log.txt file is appended. Any ideas why it fails to function when cron runs it?
#!/usr/local/bin/php
<?php
$newline=chr(10);
$now=time().$newline;
$handle=fopen("log.txt", "a+");
fwrite($handle, $now);
fclose($handle);
?>
I have also tried a version with "echo $now();" before the closing tag, which causes cron to send me an email with the output of the script. The email has the expected result, so I know there's nothing wrong with the variable, and I know the script is executing.
I've also tested the script with "file_exists" and "is_writable" variations; the script sees the file, and considers it writable. If I substitute "$result=fwrite($handle, $now);" I get a result. The script clearly thinks it is updating log.txt, but when I check that file, it is unchanged.
Again, when I execute the same script from a browser, it works fine -- the log.txt file is appended. Any ideas why it fails to function when cron runs it?