0

I have this PHP script below that, for some reason, doesn't allow me to output to the browser as the script is running. This is a heavy script and will likely run for 10-15 minutes in a production environment, so I would need to be able to see the output as it is progressing rather than just a massive output right at the end.

set_time_limit(0);
ini_set("memory_limit", "256M");

apache_setenv('no-gzip', 1);
ini_set('zlib.output_compression', 0);
ini_set('implicit_flush', 1);

ob_end_flush();
ob_flush();
flush();
ob_start();

echo "Script STARTED at " . date("Y-m-d H:i:s", time()) . "<br />";

// get all the "payments"
$result = mysql_query("SELECT * FROM payments");
while ($payment = mysql_fetch_array($result)) {
    // do a SQL update in here
    echo "Write out progress here...<br />";
}

echo "Script ENDED at " . date("Y-m-d H:i:s", time()) . "<br />";

An httpd -v gives me:

Server version: Apache/2.2.3 Server built: Oct 20 2011 17:00:12

and I am running PHP 5.3.27

I've taken some code in this script from other SO posts, but this current setup does not work.

hakre
  • 193,403
  • 52
  • 435
  • 836
crmpicco
  • 16,605
  • 26
  • 134
  • 210
  • 1
    U need to flush after your `echo`'s. There is an option where every `echo`will flush automatically tho (can't remember it atm) – DarkBee Apr 03 '14 at 11:01
  • @DarkBee Is that an `ob_flush` or just a `flush`? Does it have to go after every `echo` (I have more than one in this script)? – crmpicco Apr 03 '14 at 11:05
  • 1
    Both, u can do it after a serveral amount of `echo`'s if u want :) – DarkBee Apr 03 '14 at 11:07

1 Answers1

1

you need to flush after each echo with

flush(); ob_flush();

Although it might not work because of server setting

 flush() may not be able to override the buffering scheme of your web server and it has      no effect on any client-side buffering in the browser. [...]

 Several servers, especially on Win32, will still buffer the output from your script until it terminates before transmitting the results to the browser.

 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.

from stackoverflow.com flush documentation

Community
  • 1
  • 1
Adam Fischer
  • 1,075
  • 11
  • 23
  • Thank you. This worked for me, I should add I also had to uncomment this in php.ini `output_buffering Default Value: Off` More info at http://stackoverflow.com/questions/5324963/how-to-correctly-show-output-at-every-echo-on-all-browsers?rq=1 – crmpicco Apr 03 '14 at 11:12