PHP脚本运行输出缓冲时无法输出到浏览器

时间:2014-04-03 10:58:45

标签: php apache output output-buffering

我在下面有这个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帖子中获取了此脚本中的一些代码,但此当前设置无效。

1 个答案:

答案 0 :(得分: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