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

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

Hi Please View Below Code :

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

    You need to add a .htaccess file to disable gzip output

    <IfModule mod_env.c>
        SetEnv no-gzip 1
    </IfModule>
    
    0 讨论(0)
  • 2020-12-07 01:09

    Some browsers need to receive at least 256 characters before they start to render. Have you already tried to stuff more output like:

    echo str_repeat('&nbsp;', 50) . "$i<br />\n";
    

    EDIT:

    Under Apache/2.2.11 (Win32) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8i PHP/5.2.9 I was able to reproduce the problem of the OP by setting

    zlib.output_compression = On
    

    Turning it off again by

    zlib.output_compression = Off
    

    made the script work as wanted.

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

    It is correct. Works fine for me from CLI running PHP 5.3.3. If it's not working for you, your PHP install may have output buffering disabled.

    I would also suggest putting ob_end_flush() at the end of your script to close the output buffer.

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

    Try removing the call to ob_start() on your first line : there is no need for you to enable output buffering -- and it probably causes troubles, here.


    I've tested your code :

    • If ob_start() is called on the first line, I only see the output when the script finishes, after 10 seconds
    • If I remove that call to ob_start(), then, I see one line of output every second, as soon as it's displayed on the standard output.
    0 讨论(0)
  • 2020-12-07 01:15

    I've discovered that this was due to Apache's gzip compression being in use for my case.

    To turn gzip off for the 'flushing' script only, I created a new .htaccess file in the directory where the continuous output script resides, with the following:

    <IfModule mod_env.c>
        SetEnv no-gzip 1
    </IfModule>
    

    Flushing is working as expected again.

    0 讨论(0)
  • One sneaky issue with IE8 and flush(); is that if you're "flushing" out rows in a table. IE will only display tables when they're complete. This was my issue, and changing containers from table rows to divs solved the problem.

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