PHP脚本超时是否会在打印/刷新语句中发生?


Will PHP script timeout occur during a print / flush statement?

我有一个下载脚本,它检查了几件事,然后以8kb的块传输文件。

执行传输的循环看起来像:


$file = @fopen($file_path,"rb");
if ($file) {
  while(!feof($file)) {
    set_time_limit(60);
    print(fread($file, 1024*8));
    flush();
    if (connection_status()!=0) {
      @fclose($file);
      die();
    }
  }
  @fclose($file);
}

我写了一个小应用程序,模拟了非常慢的下载速度。等待2分钟后继续下载。考虑到我设置了60秒的时间限制,我预计脚本会超时。这种情况不会发生,下载将继续,直到完成。似乎花在打印/刷新上的时间不计入脚本执行时间。这是正确的吗?是否有更好的方法将文件发送到客户端/浏览器,以便我可以为打印/冲洗命令指定时间限制?

From set_time_limit():

The set_time_limit() function and the configuration directive max_execution_time
only affect the execution time of the script itself. Any time spent on activity
that happens outside the execution of the script such as system calls using system(),
stream operations, database queries, etc. is not included when determining the
maximum time that the script has been running. This is not true on Windows where
the measured time is real.

因此,看起来您可以通过调用time()函数来测量实时时间的流逝,沿着以下行:

$start = time();
while (something) {
    // do something
    if( time()-$start > 60) die();
}

或者您可以使用Windows。我更喜欢第一个选项:p