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!

4

1 回答 1

0

可能没有锁定。
为什么要手动写入文件,然后使用error_log

  • 考虑file_put_contents()改为FILE_APPEND|FILE_LOCK。这仅附加到文件的末尾并负责锁定。

  • 启用error_reporting并重定向stderr到单独的文件以了解更多信息。

    php test.php 2> php_errors.txt &

于 2013-02-13T02:26:55.007 回答