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

时间:2011-10-18 12:02:16

标签: php download timeout flush

我有一个下载脚本,可以检查几件事情,然后将文件流传输到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秒的时间限制,脚本会超时。这种情况不会发生,下载会一直持续到完成。似乎在打印/刷新中花费的时间不计入脚本执行时间。它是否正确?有没有更好的方法将文件发送到客户端/浏览器,以便我可以为print / flush命令指定时间限制?

1 个答案:

答案 0 :(得分:1)

来自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