Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.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 如何在10秒钟内逐行读取文件?_Php_Time_While Loop - Fatal编程技术网

Php 如何在10秒钟内逐行读取文件?

Php 如何在10秒钟内逐行读取文件?,php,time,while-loop,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:") == fa

我正在做一个项目,我需要阅读日志并检查错误。我使用的是这样的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($count<10){
$count++;
#如果引发异常,请停止等待
$sdata=file($logfile);
$lastline=$sdata[计数($sdata)-1];
if(startsWith($lastline,“Running:”)==false或startsWith($lastline,“Executing:”)==false){break;}
睡眠(1);
}

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

这是否满足您的需要

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

}
$time=time();
$stop=$time+10;
而($time<$stop){
$time=time();
#如果引发异常,请停止等待
$sdata=file($logfile);
$lastline=结束($sdata);
if(startsWith($lastline,“Running:”)==false或startsWith($lastline,“Executing:”)==false){break;}
}

您是在尝试每秒查看一次文件的更改,持续10秒,还是在尝试尽可能频繁地轮询文件,持续10秒?我正在尽可能频繁地轮询文件,持续10秒。我删除了循环,因为您只在最后一行进行了轮询谢谢!工作完美!请注意,如果您的程序的输出速度比您在内存中读取整个文件的速度快,那么这种方法不能保证您将处理每一行。日志文件越大,丢失一行的可能性越大。