6

我在更新脚本时遇到问题。它运行了几个小时,所以我希望它实时输出到文本文件。

我开始文件

ob_start();

然后在while循环中(因为它遍历数据库的记录)我有这个

$size=ob_get_length();
if ($size > 0)
{
    $content = ob_get_contents();
    logit($contents);
    ob_clean();
}

最后是 logit 函数

function logit($data)
{
    file_put_contents('log.txt', $data, FILE_APPEND);
}

但是日志文件仍然是空的。我究竟做错了什么?

4

3 回答 3

6

尝试

logit($content);
//           ^^ Note the missing s
于 2010-07-22T11:05:57.907 回答
0

$contents不是同一个变量$content

于 2010-07-22T11:07:17.107 回答
0

对于任何来这里寻找功能的人,我今天写了一个很好的:

//buffer php outout between consecutive calls and optionally store it to a file:
function buffer( $toFilePath=0, $appendToFile=0 ){
    $status = ob_get_status ();
    if($status['level']===1) return ob_start(); //start the buffer
    $res = ob_get_contents();
    ob_end_clean();
    if($toFilePath) file_put_contents($toFilePath, $res, ($appendToFile ? FILE_APPEND : null));
    return $res;
}

示例用法:

buffer(); //start the buffer

echo(12345); //log some stuff
echo(678910);

$log = buffer('mylog.txt',1); //add these lines to a file (optional)

echo('Heres the latest log:'.$log);
于 2018-05-17T13:02:40.910 回答