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.

3
  • 1
    U need to flush after your echo's. There is an option where every echowill flush automatically tho (can't remember it atm) Commented Apr 3, 2014 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)? Commented Apr 3, 2014 at 11:05
  • 1
    Both, u can do it after a serveral amount of echo's if u want :) Commented Apr 3, 2014 at 11:07

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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 stackoverflow.com/questions/5324963/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.