1

I am doing a lot of processing at my PHP page and I want to display progress information to the user, for instance, finished 10% 20% and so. What is happening now is that all the data is displayed at once after processing is done, how can I display it right away!

I tried to set comment out output buffer in php.ini and I tried to use flush() after echo statements, not working, any suggestions?

Here is my code:

ob_start();
while ($line = read_file_line("c:/1.txt")) {  
  $read_lines_count++;
  if($read_lines_count % 100 == 0) {
    echo "parsed $read_lines_count <br />";
  ob_flush();
  }
}
4
  • 1
    This depends on the server configurtion as well. If you want to be sure, split the job in parts and use ajax. If ajax is an option of course. Commented Apr 11, 2014 at 18:52
  • @xd6_ I just tried it, nop :( still waits until processing is done... Commented Apr 11, 2014 at 18:52
  • @jeroen, I have access to the server, any changes I need to make in php.ini ? Commented Apr 11, 2014 at 18:53
  • 1
    @AminM I'm not sure, there is a lot more than just php involved. Commented Apr 11, 2014 at 19:07

1 Answer 1

4

First you need to call ob_start() before any code is printed.

Then echo whatever you want

call ob_flush() when you want to show the buffer on screen.

and at the end, a call to ob_end_flush() to end buffering and show output.

Make sure your php.ini has this line uncommented:

output_buffering = On

More Info : http://www.php.net/manual/en/book.outcontrol.php

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

6 Comments

@AminM you need to explicitly set output_buffering to true in the php.ini, commenting the line will set it to false (default's apache value). Do a var_dump(ini_get('output_buffering')) and it will show you the output_buffering configuration
I just did it's string '4096' (length=4)
@AminM This means that it will buffer up to 4086 bytes at most. Write in your php.ini output_buffering = On php.net/manual/en/…
yeah sorry! I meant to say I set it to on, vardump is string: 1 :(
@AminM Are you sure the file is long enough ? It might be instant if you try on your localhost or may be fast if the file is less than 1Mb on a remote host and can't even notice it's buffering.
|

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.