doucai6663
2014-04-03 10:58
浏览 87
已采纳

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

I have this PHP script below that, for some reason, doesn't allow me to output to the browser as the script is running. This is a heavy script and will likely run for 10-15 minutes in a production environment, so I would need to be able to see the output as it is progressing rather than just a massive output right at the end.

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 />";

An httpd -v gives me:

Server version: Apache/2.2.3 Server built: Oct 20 2011 17:00:12

and I am running PHP 5.3.27

I've taken some code in this script from other SO posts, but this current setup does not work.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongmu5106 2014-04-03 11:06
    已采纳

    you need to flush after each echo with

    flush(); ob_flush();
    

    Although it might not work because of server setting

     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

    点赞 打赏 评论

相关推荐 更多相似问题