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

发布于 2024-12-10 16:21:34 字数 494 浏览 0 评论 0原文

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

I've got a download script which checks a couple of things and then streams a file across in chunks of 8kb.

The loop that does the transfer looks like:


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

I wrote a small application which simulated a very slow download. It waits for 2 minutes before continuing the download. I expected that the script would time out given that I've set a 60 second time limit. This does not happen and the download continues until it has finished. It seems that the time spent in print / flush doesn't count towards the script execution time. Is this correct? Is there a better way to send the file to the client / browser such that I can specify a time limit for the print / flush command?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸢与 2024-12-17 16:21:34

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

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.

So it looks like you can either measure the passage of real time with calls to the time() function, along the lines of:

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

Or you can use Windows. I prefer the first option :p

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文