【问题标题】:unable to output to the browser whilst PHP script is running with output buffering无法在 PHP 脚本使用输出缓冲运行时输出到浏览器
【发布时间】:2014-04-03 10:58:44
【问题描述】:

我在下面有这个 PHP 脚本,由于某种原因,它不允许我在脚本运行时输出到浏览器。这是一个繁重的脚本,可能会在生产环境中运行 10 到 15 分钟,所以我需要能够看到正在运行的输出,而不是最后的大量输出。

set_time_limit(0);
ini_set("memory_limit", "256M");

apache_setenv('no-gzip', 1);
ini_set('zlib.output_compression', 0);
ini_set('implicit_flush', 1);

ob_end_flush();
ob_flush();
flush();
ob_start();

echo "Script STARTED at " . date("Y-m-d H:i:s", time()) . "<br />";

// get all the "payments"
$result = mysql_query("SELECT * FROM payments");
while ($payment = mysql_fetch_array($result)) {
    // do a SQL update in here
    echo "Write out progress here...<br />";
}

echo "Script ENDED at " . date("Y-m-d H:i:s", time()) . "<br />";

httpd -v 给了我:

服务器版本:Apache/2.2.3 服务器搭建时间:2011 年 10 月 20 日 17:00:12

我正在运行 PHP 5.3.27

我从其他 SO 帖子中获取了此脚本中的一些代码,但当前的设置不起作用。

【问题讨论】:

  • 你需要在echo 之后刷新。有一个选项可以让每个 echo 自动刷新(atm 不记得了)
  • @DarkBee 这是ob_flush 还是flush?是否必须针对每个echo(我在这个脚本中有不止一个)?
  • 两者,如果你愿意,你可以在echo 的服务器数量之后做到这一点:)

标签: php apache output output-buffering


【解决方案1】:

你需要在每次回显后刷新

flush(); ob_flush();

虽然由于服务器设置可能无法正常工作

 flush() may not be able to override the buffering scheme of your web server and it has      no effect on any client-side buffering in the browser. [...]

 Several servers, especially on Win32, will still buffer the output from your script until it terminates before transmitting the results to the browser.

 Server modules for Apache like mod_gzip may do buffering of their own that will cause flush() to not result in data being sent immediately to the client.

from stackoverflow.com flush documentation

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-12-30
  • 2017-08-23
  • 2010-11-24
  • 1970-01-01
  • 2014-05-25
  • 1970-01-01
  • 1970-01-01
  • 2014-10-09
相关资源
最近更新 更多