【问题标题】:Will PHP script timeout occur during a print / flush statement?在打印/刷新语句期间会发生 PHP 脚本超时吗?
【发布时间】:2011-10-18 12:02:15
【问题描述】:

我有一个下载脚本,它会检查几项内容,然后以 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 秒的时间限制,我预计脚本会超时。这不会发生,下载会一直持续到完成。似乎打印/刷新所花费的时间不计入脚本执行时间。这个对吗?有没有更好的方法将文件发送到客户端/浏览器,以便我可以为打印/刷新命令指定时间限制?

【问题讨论】:

    标签: php download timeout flush


    【解决方案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

    【讨论】:

    • 很好,但是在flush语句阻止脚本执行的情况下,这对我没有帮助。如果它永远不会返回,我将无法进行时间检查......
    • 只是为了兴趣。我用 IIS 在 Windows 上运行了测试,我也没有死(它在很长一段时间后才死掉,但我希望 IIS 是罪魁祸首,而不是 PHP 执行时间限制)。生产服务器正在运行 Linux 和 Apache。
    猜你喜欢
    • 2014-03-26
    • 1970-01-01
    • 2013-01-03
    • 1970-01-01
    • 1970-01-01
    • 2010-11-18
    • 2011-12-30
    • 1970-01-01
    • 2020-01-12
    相关资源
    最近更新 更多