Calling ob_flush() and flush(), yet browser doesn't show any output until script finishes

后端 未结 12 2799
离开以前
离开以前 2020-12-07 00:55

Hi Please View Below Code :

\\n\";
for( $i = 0 ; $i < 10 ; $i++ )
{
    echo \"$i
\\n\";
相关标签:
12条回答
  • 2020-12-07 00:57

    Hey man I was also got stuck in this problem and finally got the correct solution here it is for you

    you have to add content type for your page you can do that by two ways 1. using html tag

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    

    Ex.

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Wp Migration</title>
    </head>
    <body>
    <?php 
    for($i=0;$i<70;$i++)
    {
    echo 'printing...<br>';
    ob_flush();
    flush();
    sleep(3);
    }
    ?>
    </body>
    </html>
    
    1. using php header function

      <?php header( 'Content-type: text/html; charset=utf-8' ); ?>

    Ex.

    <?php 
    header( 'Content-type: text/html; charset=utf-8' );
    for($i=0;$i<70;$i++)
    {
    echo 'printing...<br>';
    ob_flush();
    flush();
    sleep(3);
    }
    ?>
    

    All the best

    0 讨论(0)
  • 2020-12-07 00:57

    This flow works with Laravel too

    ob_implicit_flush(true);
    echo "Processing ... "; // Or give out JSON output
    ob_flush();
    sleep(5); //A time-consuming synchronous process (SMTP mail, maybe?)
    echo "Done";
    
    0 讨论(0)
  • 2020-12-07 00:58

    Using Chrome, I found out that many more bytes are required to by-pass the browser's buffer. In my case 4096 bytes was fine:

    echo str_repeat(' ', 4096);
    

    Also, adding some HTML element at the beginning also seemed to be mandatory:

    echo $content . '<br />';
    
    0 讨论(0)
  • 2020-12-07 00:58

    For people using FCGI / fast cgi.

    FcgidOutputBufferSize 0
    
    0 讨论(0)
  • 2020-12-07 01:00

    On my system it appears that FF4 needs more than 256 bytes to start rendering what is arriving from the server side, then i resolved with this at the beginning:

    while (@ob_end_flush());
    echo(str_repeat(' ',1024));
    // ...etc...
    
    0 讨论(0)
  • 2020-12-07 01:03

    I am using laravel framework and buffering did not work but. This is solution :

    header( 'Content-type: text/html; charset=utf-8' );
    ob_start();
    
    ob_end_flush();
    ob_flush();
    flush();
    for($i = 1;$i<= 5;$i++){
        echo $i;
        ob_flush();
        flush();
    
        sleep(3);
    }
    

    You have to use first ob_end_flush();

    0 讨论(0)
提交回复
热议问题