PHP flushing output as soon as you call echo

前端 未结 6 756
有刺的猬
有刺的猬 2020-12-22 08:47

I thought flush(); would work, at least from what Google/Stackoverflow tell me, but on my Windows WAMP (Windows, Apache, MySQL, PHP) system it doesn\'t work.

6条回答
  •  粉色の甜心
    2020-12-22 09:29

    So that's what I found out:

    Flush would not work under Apache's mod_gzip or Nginx's gzip because, logically, it is gzipping the content, and to do that it must buffer content to gzip it. Any sort of web server gzipping would affect this. In short, at the server side, we need to disable gzip and decrease the fastcgi buffer size. So:

    • In php.ini:

      . output_buffering = Off

      . zlib.output_compression = Off

    • In nginx.conf:

      . gzip off;

      . proxy_buffering off;

    Also have this lines at hand, specially if you don't have acces to php.ini:

    • @ini_set('zlib.output_compression',0);

    • @ini_set('implicit_flush',1);

    • @ob_end_clean();

    • set_time_limit(0);

    Last, if you have it, coment the code bellow:

    • ob_start('ob_gzhandler');

    • ob_flush();

    PHP test code:

    ob_implicit_flush(1);
    
    for($i=0; $i<10; $i++){
        echo $i;
    
        //this is for the buffer achieve the minimum size in order to flush data
        echo str_repeat(' ',1024*64);
    
        sleep(1);
    }
    

提交回复
热议问题