【问题标题】:PHP output buffering to text filePHP输出缓冲到文本文件
【发布时间】:2010-07-22 11:02:20
【问题描述】:

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

我以文档开头

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

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

【问题讨论】:

  • 你检查日志文件路径的权限了吗?
  • 输入错误。 :) $contents => $content

标签: php output-buffering


【解决方案1】:

试试

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

【讨论】:

    【解决方案2】:

    $contents$content 不是同一个变量。

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-27
        • 2011-02-06
        • 1970-01-01
        • 2017-04-06
        • 1970-01-01
        相关资源
        最近更新 更多