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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 手机连接电脑热点显示无ip分配
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大