Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP输出缓冲到文本文件_Php_Output Buffering - Fatal编程技术网

PHP输出缓冲到文本文件

PHP输出缓冲到文本文件,php,output-buffering,Php,Output Buffering,我在更新脚本时遇到问题。它运行了几个小时,所以我希望它能实时输出到一个文本文件 我以 ob_start(); 然后在while循环中(当它遍历数据库的记录时),我有这样一个 $size=ob_get_length(); if ($size > 0) { $content = ob_get_contents(); logit($contents); ob_clean(); } 最后是logit函数 function logit($data) { file_

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

我以

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);
}
但是,日志文件仍然为空。我做错了什么?

试试看

logit($content);
//           ^^ Note the missing s

$contents
$content

不是同一个变量。对于来这里寻找此函数的任何人,我今天写了一篇很好的文章:

//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);

您是否检查了日志文件路径上的权限?键入错误:)<代码>$contents=>
$content