如何逐行读取文件10秒?

时间:2014-01-13 00:16:04

标签: php time while-loop

我正在开展一个项目,我需要阅读日志并检查错误。我像这样使用while循环:

 while($count < 10) { 
      $count++;
      # Stop waiting if an exception is thrown
      $sdata = file($logfile);
      $lastline = $sdata[count($sdata)-1];
      if(startsWith($lastline, "Running:") == false or startsWith($lastline,"Executing:") == false) { break; }
      sleep(1);  
 }

这是每秒检查一次日志,但是,我需要在它进入时读取每一行。如何在保持while循环的超时值的同时读取日志的最后一行?

1 个答案:

答案 0 :(得分:0)

这可以满足您的需求吗?

$time = time();
$stop = $time+10;
while($time < $stop) { 
    $time = time();
    # Stop waiting if an exception is thrown
    $sdata = file($logfile);

    $lastline = end($sdata);
    if(startsWith($lastline, "Running:") == false or startsWith($lastline,"Executing:") == false) { break; }

}