PHP flush the output to browser

淺唱寂寞╮ 提交于 2019-12-02 05:51:42

问题


I work on a PHP project and I use flush().

I did a lot of search and found that PHP sends long outputs of scripts to the browser in chunk parts and does not send all the huge data when the script terminates.

I want to know the size of this data, I mean how many bytes the output must be for PHP to send them to browser.


回答1:


It's not only PHP that chunks the data; it's actually the job of Apache (or Tomcat etc) to do this. That's why the default is to turn off the "chunking" in PHP and leave it to Apache. Even if you force a flush from PHP, it still can get trapped by Apache. From the manual:

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. It also doesn't affect PHP's userspace output buffering mechanism. This means you will have to call both ob_flush() and flush() to flush the ob output buffers if you are using those.

There's a Wikipedia article on transfer encoding / chunking: http://en.wikipedia.org/wiki/Chunked_transfer_encoding

Apache gets more complicated with GZIP or deflate encoding; you'll need to hit an apache server as to how you chan configure it.




回答2:


i think you are wrong see this code

echo str_repeat(' ',1024);
for($i=0;$i<10;$i++){
echo $i;
flush();
sleep(1);   

if you run it see that every 1 byte sent to browser and print //the str_repeat is for browser buffer for showing data and nothing else



来源:https://stackoverflow.com/questions/11857577/php-flush-the-output-to-browser

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!