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

12

22

Hi Please View Below Code :

<?php
ob_start();

echo "Start ...<br />\n";
for( $i = 0 ; $i < 10 ; $i++ )
{
    echo "$i<br />\n";
    ob_flush(); 
    flush();
    sleep(1);
}
echo "End ...<br />\n";
?>

It's Incorrect ? i'm tested it but my output show when script is done, have any solution ?

Hydrolysis answered 24/4, 2011 at 13:59 Comment(14)
Have you tested this in multiple browsers? Removing ob_start() might be one piece of the puzzle, but you might also need to do something similar to what Jürgen suggested if it still doesn't work in IE.Vmail
Tested it on FireFox 4, IE9, IE8, IE8, IE6, Chrome . all of theme doesen't workHydrolysis
Hm, just a wild guess, but do you have mod_gzip active for the mime type of your script?Combo
No, not set Content-Encoding header in pageHydrolysis
Just want to make that sure: in your php.ini you've set output_buffering = On, and not to a value (e.g. output_buffering = 4096), zlib.output_compression = Off and no panel or .htaccess file does overwrite this values?Combo
tested this with LAMP and firefox 4 IE8 Chrome, only work in IE8Raccoon
@Jürgen Thelen : Checked, all of theme is ok . i'm wondering from this problem !Hydrolysis
@hope_is_grim, i have'nt LAMP, but doesn't must difrent width LAMP and WAMPHydrolysis
@Davood Jafari: Sorry, now I'm running out of ideas.. :( EDIT: you don't have a proxy between client and server, haven't you?Combo
@Davood Jafari: Another thing came to my mind: maybe your Apache is using modules that buffer output by themselves. You could try disabling Apache modules one by one to see if a specific module is causing the problem.Combo
Really Thank you about Continuous. Testet it and inform youHydrolysis
@JürgenThelen gzipping is disabledGrumous
Were you able to solve this? I seem to have run into this problem too.Bolshevism
@Bolshevism unfortunately no :(Hydrolysis
C
14

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

Clostridium answered 12/6, 2013 at 13:15 Comment(2)
So it seems the browser gets the content with or without the content type line, however if you don't have it, the browser simply changes the loading indicator to "receiving data" without displaying anything. Once you send the content type first, sending any amount of data and flushing will work =))) I confirmed the same behavior exists when running on Apache and IIS (regardless of output buffering settings) so it's definitely due to the browser's own requirements.Raimundo
ob_flush(): Failed to flush buffer. No buffer to flushBireme
L
6

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.

Lavernelaverock answered 24/4, 2011 at 14:48 Comment(0)
C
4

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.
Carbone answered 24/4, 2011 at 14:4 Comment(6)
If you remove ob_start(), then you should remove all the ob_ calls as well.Alta
Not sure about that (If I remove the call to ob_flush(), I only see the output after the 10 seconds delay) ; maybe it's because output_buffering is enabled in my php.ini, though ;;; see what's written on fr.php.net/flush tooCarbone
are you sure ? tested in browser ?Hydrolysis
is possible give me link to view result ?Hydrolysis
I tested on my local computer (and the Apache I've got on it is not accessible from the Internet) ;; anyway, I just copy-pasted your code, and commented the ob_start() call.Carbone
I Tested it on local ( WAMP5 ) and Apache Web Server, both return output after 10 second ! i even tested this code on 2 server, but problem still exist .Hydrolysis
L
2

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 />';
Leucoplast answered 24/5, 2012 at 18:14 Comment(1)
Probably better example is here: php.net/manual/en/function.flush.php#54841Exponential
H
1

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...
Haemic answered 16/6, 2011 at 14:52 Comment(0)
S
1

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.

Sputum answered 5/2, 2015 at 13:10 Comment(0)
C
1

For people using FCGI / fast cgi.

FcgidOutputBufferSize 0
Casey answered 13/2, 2015 at 7:22 Comment(0)
A
0

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.

Alta answered 24/4, 2011 at 14:8 Comment(2)
In Command Line Interface Worked, But Browser ... ! and output buffering is enable on my server :(Hydrolysis
i have ob_end_flush() in placeGrumous
U
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.

Undertrick answered 7/8, 2013 at 8:48 Comment(0)
H
0

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

<IfModule mod_env.c>
    SetEnv no-gzip 1
</IfModule>
Hypogene answered 28/11, 2015 at 2:17 Comment(0)
S
0

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();

Sartre answered 20/8, 2016 at 19:7 Comment(0)
T
0

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";
Transcaucasia answered 22/3, 2017 at 6:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.