Is it possible to echo each time the loop is executed? For example:
foreach(range(1,9) as $n){
echo $n."\n";
sleep(1);
}
Instead of printing everything when the loop is finished, I'd like to see it printing each result per time.
Join Stack Overflow to learn, share knowledge, and build your career.
The easiest way to eliminate nginx's buffering is by emitting a header:
header('X-Accel-Buffering: no');
This eliminates both proxy_buffering
and (if you have nginx >= 1.5.6), fastcgi_buffering
. The fastcgi bit is crucial if you're using php-fpm. The header is also far more convenient to do on an as-needed basis.
set_time_limit(0)
, sending only this header, no content type header, followed by my echo'd HTML (or require_once()), followed by ob_flush()
, and then flush()
. I could then do my long task (an eCommerce task) and then output the rest of the HTML when done to close the page, which adds some Javascript that redirects the page immediately to their order receipt.
– Volomike
Apr 24 '17 at 5:23
FINAL SOLUTION
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);
}
Related:
Easy solution on nginx server:
fastcgi_keep_conn on; # < solution
proxy_buffering off;
gzip off;
http
, server
or location
contexts. So you can be very selective where it applies.
– Majid Fouladpour
Sep 17 '19 at 9:11
I didn't want to have to turn off gzip for the whole server or a whole directory, just for a few scripts, in a few specific cases.
All you need is this before anything is echo'ed:
header('Content-Encoding: none;');
Then do the flush as normal:
ob_end_flush();
flush();
Nginx seems to pick up on the encoding having been turned off and doesn't gzip.
header('X-Accel-Buffering: no');
was needed too.
– kbtz
Aug 24 '15 at 11:50
header('Content-Encoding: none;');
worked for me. No need for ob_end_flush()
or flush()
, just make sure the output is big enough by using echo str_repeat(' ', 1024*64);
– user670839
Feb 14 '17 at 8:55
You need to flush the php's buffer to the browser
foreach(range(1,9) as $n){
echo $n."\n";
flush();
sleep(1);
}
if(ob_get_level() > 0) ob_flush();
– meze
Feb 2 '11 at 4:50
I found that you can set:
header("Content-Encoding:identity");
in your php script to disable nginx gzipping without having to modify the nginx.conf
You can accomplish this by flushing the output buffer in the middle of the loop.
Example:
ob_start();
foreach(range(1,9) as $n){
echo $n."\n";
ob_flush();
flush();
sleep(1);
}
Note that your php.ini settings can affect whether this will work or not if you have zlib compression turned on
I had a gzip problem comming from my php-fpm engine. this code is the only one working for me :
function myEchoFlush_init() {
ini_set('zlib.output_compression', 0);
ini_set('output_buffering', 'Off');
ini_set('output_handler', '');
ini_set('implicit_flush', 1);
ob_implicit_flush(1);
ob_end_clean();
header('Content-Encoding: none;');
}
function myEchoFlush($str) {
echo $str . str_repeat(' ', ini_get('output_buffering') * 4) . "<br>\n";
}
This is my test function : it checks max_execution_time :
public function timeOut($time = 1, $max = 0) {
myEchoFlush_init();
if ($max) ini_set('max_execution_time', $max);
myEchoFlush("Starting infinite loop for $time seconds. It shouldn't exceed : " . (ini_get('max_execution_time')));
$start = microtime(true);
$lastTick = 1;
while (true) {
$tick = ceil(microtime(true) - $start);
if ($tick > $lastTick) {
myEchoFlush(microtime(true) - $start);
$lastTick = $tick;
}
if ($tick > $time) break;
}
echo "OK";
}