How to flush output after each `echo` call? - php
I have a php script that only produces logs to the client.
When I echo something, I want it to be transferred to client on-the-fly.
(Because while the script is processing, the page is blank)
I had already played around with ob_start() and ob_flush(), but they didn't work.
What's the best solution?
PS: it is a little dirty to put a flush at the end of the echo call...
EDIT: Neither the Answers worked, PHP or Apache Fault?
I've gotten the same issue and one of the posted example in the manual worked. A character set must be specified as one of the posters here already mentioned. http://www.php.net/manual/en/function.ob-flush.php#109314
header( 'Content-type: text/html; charset=utf-8' );
echo 'Begin ...<br />';
for( $i = 0 ; $i < 10 ; $i++ )
{
echo $i . '<br />';
ob_flush();
flush();
sleep(1);
}
echo 'End ...<br />';
Edit:
I was reading the comments on the manual page and came across a bug that states that ob_implicit_flush does not work and the following is a workaround for it:
ob_end_flush();
# CODE THAT NEEDS IMMEDIATE FLUSHING
ob_start();
If this does not work then what may even be happening is that the client does not receive the packet from the server until the server has built up enough characters to send what it considers a packet worth sending.
Old Answer:
You could use ob_implicit_flush which will tell output buffering to turn off buffering for a while:
ob_implicit_flush(true);
# CODE THAT NEEDS IMMEDIATE FLUSHING
ob_implicit_flush(false);
So here'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 these lines at hand, especially if you don't have access 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, comment 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 to make the buffer achieve the minimum size in order to flush data
echo str_repeat(' ',1024*64);
sleep(1);
}
For those coming in 2018:
The ONLY Solution worked for me:
<?php
if (ob_get_level() == 0) ob_start();
for ($i = 0; $i<10; $i++){
echo "<br> Line to show.";
echo str_pad('',4096)."\n";
ob_flush();
flush();
sleep(2);
}
echo "Done.";
ob_end_flush();
?>
and its very important to keep de "4096" part because it seems that "fills" the buffer...
Flushing seemingly failing to work is a side effect of automatic character set detection.
The browser will not display anything until it knows the character set to display it in, and if you don't specify the character set, it need tries to guess it. The problem being that it can't make a good guess without enough data, which is why browsers seem to have this 1024 byte (or similar) buffer they need filled before displaying anything.
The solution is therefore to make sure the browser doesn't have to guess the character set.
If you're sending text, add a '; charset=utf-8' to its content type, and if it's HTML, add the character set to the appropriate meta tag.
what you want is the flush method.
example:
echo "log to client";
flush();
Why not make a function to echo, like this:
function fecho($string) {
echo $string;
ob_flush();
}
One thing that is not often mentionned is gzip compression that keeps turned ON because of details in various hosting environments.
Here is a modern approach, working with PHP-FPM as Fast CGI, which does not need .htaccess rewrite rule or environment variable :
In php.ini or .user.ini :
output_buffering = 0
zlib.output_compression = 0
implicit_flush = true
output_handler =
In PHP script :
header('Content-Encoding: none'); // Disable gzip compression
ob_end_flush(); // Stop buffer
ob_implicit_flush(1); // Implicit flush at each output command
See this comment on official PHP doc for ob_end_flush() need.
I had a similar thing to do. Using
// ini_set("output_buffering", 0); // off
ini_set("zlib.output_compression", 0); // off
ini_set("implicit_flush", 1); // on
did make the output flushing frequent in my case.
But I had to flush the output right at a particular point(in a loop that I run), so using both
ob_flush();
flush();
together worked for me.
I wasn't able to
turn off "output_buffering" with
ini_set(...), had to turn it directly
in php.ini, phpinfo() shows its setting
as "no value" when turned off, is that
normal? .
header( 'X-Accel-Buffering: no' );
header( 'Content-Type: text/html; charset=utf-8' );
echo 'text to display';
echo '<span style="display: none;">' . str_repeat ( ' ', 4096 ) . '</span>';
flush();
usleep( 10 );
The correct function to use is flush().
<html>
<body>
<p>
Hello! I am waiting for the next message...<br />
<?php flush(); sleep(5); ?>
I am the next message!<br />
<?php flush(); sleep(5); ?>
And I am the last message. Good bye.
</p>
</body>
</html>
Please note that there is a "problem" with IE, which only outputs the flushed content when it is at least 256 byte, so your first part of the page needs to be at least 256 byte.
This works fine for me (Apache 2.4/PHP 7.0):
#ob_end_clean();
echo "lorem ipsum...";
flush();
sleep(5);
echo "<br>dolor...";
flush();
sleep(5);
echo "<br>sit amet";
Anti-virus software may also be interfering with output flushing. In my case, Kaspersky Anti-Virus 2013 was holding data chunks before sending it to the browser, even though I was using an accepted solution.
Sometimes, the problem come from Apache settings. Apache can be set to gzip the output.
In the file .htaccess you can add for instance :
SetEnv no-gzip 1
Try this:
while (#ob_end_flush());
ob_implicit_flush(true);
echo "first line visible to the browser";
echo "<br />";
sleep(5);
echo "second line visible to the browser after 5 secs";
Just notice that this way you're actually disabling the output buffer for your current script. I guess you can reenable it with ob_start() (i'm not sure).
Important thing is that by disabling your output buffer like above, you will not be able to redirect your php script anymore using the header() function, because php can sent only once per script execution http headers.
You can however redirect using javascript. Just let your php script echo following lines when it comes to that:
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
echo '</noscript>';
exit;
Note if you are on certain shared hosting sites like Dreamhost you can't disable PHP output buffering at all without going through different routes:
Changing the output buffer cache If you are using PHP FastCGI, the PHP
functions flush(), ob_flush(), and ob_implicit_flush() will not
function as expected. By default, output is buffered at a higher level
than PHP (specifically, by the Apache module mod_deflate which is
similar in form/function to mod_gzip).
If you need unbuffered output, you must either use CGI (instead of
FastCGI) or contact support to request that mod_deflate is disabled
for your site.
https://help.dreamhost.com/hc/en-us/articles/214202188-PHP-overview
I'm late to the discussion but I read that many people are saying appending flush(); at the end of each code looks dirty, and they are right.
Best solution is to disable deflate, gzip and all buffering from Apache, intermediate handlers and PHP. Then in your php.ini you should have:
output_buffering = Off
zlib.output_compression = Off
implicit_flush = Off
Temporary solution is to have this in your php.ini IF you can solve your problem with flush(); but you think it is dirty and ugly to put it everywhere.
implicit_flush = On
If you only put it above in your php.ini, you don't need to put flush(); in your code anymore.
This is my code: (work for PHP7)
private function closeConnection()
{
#apache_setenv('no-gzip', 1);
#ini_set('zlib.output_compression', 0);
#ini_set('implicit_flush', 1);
ignore_user_abort(true);
set_time_limit(0);
ob_start();
// do initial processing here
echo json_encode(['ans' => true]);
header('Connection: close');
header('Content-Length: ' . ob_get_length());
ob_end_flush();
ob_flush();
flush();
}
Related
PHP output buffering issues
since two days i'm trying to stop the buffer on my server, i disabled the output_buffering in php.ini i checked it was disabled with phpinfo(). Under xampp(Localhost) it works like a charm, same testing code(below), the code runs without waiting for everything to be finished, no buffer, a dream =) On my server the output_buffering show me No value in phpinfo() so i think it's disabled, but still it's not workingn i need to wait until the loop finish his work, anyway to make this work like on my xampp config ? thanks ! testing code here : for($i=1; $i<=5000; $i++){ echo $i."<br>"; flush(); usleep(1000); } ps : i tested with php 5.6 & php7 on Debian and Ubuntu, my xampp is naturally running on windows(10)
You need to use ob_flush() and flush() What's the difference you may ask? That's a good question.
Modern browsers don't display anything until the body of the response contains a certain amount of data (about 1024 bytes). The following may look a bit hacky - but like this it works as expected: <?php echo '<!-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx -->'; flush(); for($i=1; $i<=5000; $i++) { echo $i."<br>"; flush(); usleep(1000); } ?>
PHP waits for the whole script to complete and combines all the echo together
I have a PHP code as below. When I run this on Apache (Apache/2.2.17 ), the result is that it prints 'Program started....' and then waits for 5 seconds and then print 'Script complete.' when I run this on a browser. <?php header( 'Content-type: text/html; charset=utf-8' ); set_time_limit(0); ob_implicit_flush(TRUE); echo 'Program started....'; ob_flush(); sleep(5);//making the script to wait for 5 seconds echo 'Script complete.'; ?> When I run the same above code on IIS (IIS 8), I program waits for 5 seconds and then prints 'Program started....Script complete.' at the end of execution. How can I make the code to behave the same on IIS as i does on Apache.
The comments in the PHP documentation for flush note several items in apache to check that may prevent flush() working properly: Server modules for Apache like mod_gzip may do buffering of their own that will cause flush() to not result in data being sent immediately to the client. See http://php.net/manual/en/function.flush.php and do a find for "apache" for items to try disabling. Example from commenter mandor at mandor dot net: This is what I use to turn off pretty much anything that could cause unwanted output buffering and turn on implicit flush: #apache_setenv('no-gzip', 1); #ini_set('zlib.output_compression', 0); #ini_set('implicit_flush', 1); for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); } ob_implicit_flush(1); ?>
flush() and ob_flash() no output
I've read many articles and did research online, but I cannot figure out why those commands are not working on my machine. Code get executed but it echoes when whole script is executed. I am testing it with simple code, I will easily implement it later for everything else. Here is the code: ob_start(); echo "Start ...<br />\n"; for( $i = 0 ; $i < 5 ; $i++ ) { echo $i."<br />\n"; ob_flush(); flush(); sleep(1); } echo "End ...<br />\n"; I tried using ob_end_flush(), nothing worked for me. I've checked php.ini for configuration it says: output_buffering = 4096 zlib.output_compression = Off implicit_flush = Off I don't know what is wrong, I've also read on php.net "As of August 2012, all browsers seem to show an all-or-nothing approach to buffering. In other words, while php is operating, no content can be shown." Is my code wrong? Server settings? Browsers? Is there a workaround?
Did you try? ob_implicit_flush(true);
While I don't know the real answer, you can do it like this: <?php ob_start(); ob_implicit_flush(false); echo str_repeat(' ', 1024); # rest of code here
Calling ob_flush() and flush(), yet browser doesn't show any output until script finishes
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 ?
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> 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
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(' ', 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.
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.
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 />';
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...
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.
For people using FCGI / fast cgi. FcgidOutputBufferSize 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.
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.
You need to add a .htaccess file to disable gzip output <IfModule mod_env.c> SetEnv no-gzip 1 </IfModule>
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();
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";
Run process with realtime output in PHP
I am trying to run a process on a web page that will return its output in realtime. For example if I run 'ping' process it should update my page every time it returns a new line (right now, when I use exec(command, output) I am forced to use -c option and wait until process finishes to see the output on my web page). Is it possible to do this in php? I am also wondering what is a correct way to kill this kind of process when someone is leaving the page. In case of 'ping' process I am still able to see the process running in the system monitor (what makes sense).
This worked for me: $cmd = "ping 127.0.0.1"; $descriptorspec = array( 0 => array("pipe", "r"), // stdin is a pipe that the child will read from 1 => array("pipe", "w"), // stdout is a pipe that the child will write to 2 => array("pipe", "w") // stderr is a pipe that the child will write to ); flush(); $process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array()); echo "<pre>"; if (is_resource($process)) { while ($s = fgets($pipes[1])) { print $s; flush(); } } echo "</pre>";
This is a nice way to show real time output of your shell commands: <?php header("Content-type: text/plain"); // tell php to automatically flush after every output // including lines of output produced by shell commands disable_ob(); $command = 'rsync -avz /your/directory1 /your/directory2'; system($command); You will need this function to prevent output buffering: function disable_ob() { // Turn off output buffering ini_set('output_buffering', 'off'); // Turn off PHP output compression ini_set('zlib.output_compression', false); // Implicitly flush the buffer(s) ini_set('implicit_flush', true); ob_implicit_flush(true); // Clear, and turn off output buffering while (ob_get_level() > 0) { // Get the curent level $level = ob_get_level(); // End the buffering ob_end_clean(); // If the current level has not changed, abort if (ob_get_level() == $level) break; } // Disable apache output buffering/compression if (function_exists('apache_setenv')) { apache_setenv('no-gzip', '1'); apache_setenv('dont-vary', '1'); } } It doesn't work on every server I have tried it on though, I wish I could offer advice on what to look for in your php configuration to determine whether or not you should pull your hair out trying to get this type of behavior to work on your server! Anyone else know? Here's a dummy example in plain PHP: <?php header("Content-type: text/plain"); disable_ob(); for($i=0;$i<10;$i++) { echo $i . "\n"; usleep(300000); } I hope this helps others who have googled their way here.
Checked all answers, nothing works... Found solution Here It works on windows (i think this answer is helpful for users searching over there) <?php $a = popen('ping www.google.com', 'r'); while($b = fgets($a, 2048)) { echo $b."<br>\n"; ob_flush();flush(); } pclose($a); ?>
A better solution to this old problem using modern HTML5 Server Side Events is described here: http://www.w3schools.com/html/html5_serversentevents.asp Example: http://sink.agiletoolkit.org/realtime/console Code: https://github.com/atk4/sink/blob/master/admin/page/realtime/console.php#L40 (Implemented as a module in Agile Toolkit framework)
For command-line usage: function execute($cmd) { $proc = proc_open($cmd, [['pipe','r'],['pipe','w'],['pipe','w']], $pipes); while(($line = fgets($pipes[1])) !== false) { fwrite(STDOUT,$line); } while(($line = fgets($pipes[2])) !== false) { fwrite(STDERR,$line); } fclose($pipes[0]); fclose($pipes[1]); fclose($pipes[2]); return proc_close($proc); } If you're trying to run a file, you may need to give it execute permissions first: chmod('/path/to/script',0755);
try this (tested on Windows machine + wamp server) header('Content-Encoding: none;'); set_time_limit(0); $handle = popen("<<< Your Shell Command >>>", "r"); if (ob_get_level() == 0) ob_start(); while(!feof($handle)) { $buffer = fgets($handle); $buffer = trim(htmlspecialchars($buffer)); echo $buffer . "<br />"; echo str_pad('', 4096); ob_flush(); flush(); sleep(1); } pclose($handle); ob_end_flush();
I've tried various PHP execution commands on Windows and found that they differ quite a lot. Don't work for streaming: shell_exec, exec, passthru Kind of works: proc_open, popen -- "kind of" because you cannot pass arguments to your command (i.e. wont' work with my.exe --something, will work with _my_something.bat). The best (easiest) approach is: You must make sure your exe is flushing commands (see printf flushing problem). Without this you will most likely receive batches of about 4096 bytes of text whatever you do. If you can, use header('Content-Type: text/event-stream'); (instead of header('Content-Type: text/plain; charset=...');). This will not work in all browsers/clients though! Streaming will work without this, but at least first lines will be buffered by the browser. You also might want to disable cache header('Cache-Control: no-cache');. Turn off output buffering (either in php.ini or with ini_set('output_buffering', 'off');). This might also have to be done in Apache/Nginx/whatever server you use in front. Turn of compression (either in php.ini or with ini_set('zlib.output_compression', false);). This might also have to be done in Apache/Nginx/whatever server you use in front. So in your C++ program you do something like (again, for other solutions see printf flushing problem): Logger::log(...) { printf (text); fflush(stdout); } In PHP you do something like: function setupStreaming() { // Turn off output buffering ini_set('output_buffering', 'off'); // Turn off PHP output compression ini_set('zlib.output_compression', false); // Disable Apache output buffering/compression if (function_exists('apache_setenv')) { apache_setenv('no-gzip', '1'); apache_setenv('dont-vary', '1'); } } function runStreamingCommand($cmd){ echo "\nrunning $cmd\n"; system($cmd); } ... setupStreaming(); runStreamingCommand($cmd);
First check whether flush() works for you. If it does, good, if it doesn't it probably means the web server is buffering for some reason, for example mod_gzip is enabled. For something like ping, the easiest technique is to loop within PHP, running "ping -c 1" multiple times, and calling flush() after each output. Assuming PHP is configured to abort when the HTTP connection is closed by the user (which is usually the default, or you can call ignore_user_abort(false) to make sure), then you don't need to worry about run-away ping processes either. If it's really necessary that you only run the child process once and display its output continuously, that may be more difficult -- you'd probably have to run it in the background, redirect output to a stream, and then have PHP echo that stream back to the user, interspersed with regular flush() calls.
If you're looking to run system commands via PHP look into, the exec documentation. I wouldn't recommend doing this on a high traffic site though, forking a process for each request is quite a hefty process. Some programs provide the option of writing their process id to a file such that you could check for, and terminate the process at will, but for commands like ping, I'm not sure that's possible, check the man pages. You may be better served by simply opening a socket on the port you expect to be listening (IE: port 80 for HTTP) on the remote host, that way you know everything is going well in userland, as well as on the network. If you're attempting to output binary data look into php's header function, and ensure you set the proper content-type, and content-disposition. Review the documentation, for more information on using/disabling the output buffer.
Try changing the php.ini file set "output_buffering = Off". You should be able to get the real time output on the page Use system command instead of exec.. system command will flush the output
why not just pipe the output into a log file and then use that file to return content to the client. not quite real time but perhaps good enough?
I had the same problem only could do it using Symfony Process Components ( https://symfony.com/doc/current/components/process.html ) Quick example: <?php use Symfony\Component\Process\Process; $process = new Process(['ls', '-lsa']); $process->run(function ($type, $buffer) { if (Process::ERR === $type) { echo 'ERR > '.$buffer; } else { echo 'OUT > '.$buffer; } }); ?>