0

I have a basic php script that will write/log to a text document

<?php
$filename = 'php_log.log';
$somecontent = 'writing with fwrite!'.PHP_EOL;
if (is_writable($filename)) {
    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }
    echo "Success, wrote ($somecontent) to file ($filename)";
    fclose($handle);
} else {
    echo "The file $filename is not writable";
}
error_log('logging with error_log!'.PHP_EOL,3,'php_log.log');
?>

From CLI, if i do php test.php and check php_log.log, I see both writes. But when I try php test.php &, there is nothing written to php_log.log

Is there some php setting I am unaware of? Thanks!

Andrew Park
  • 1,489
  • 1
  • 17
  • 26

1 Answers1

0

Possibly the absence of locking.
Why are writing to the file manually and then with error_log?

  • Consider file_put_contents() with FILE_APPEND|FILE_LOCK instead. That appends just to the end of the file and takes care of locking.

  • Enable error_reporting and redirect stderr to a separate file to find out more.

    php test.php 2> php_errors.txt &

mario
  • 144,265
  • 20
  • 237
  • 291