Showing progress and avoiding timeout in slow script?
Posted: Wed Jul 09, 2008 7:57 am
I'm working on a php script which performs some heavy processing on some huge SQL table. When I just do it all in one big for-loop, the script keeps people wondering if something is happening, and will eventually time out (at least the browser thinks so, even though the php may still be running at the server).
How do I show the progress, e.g. something running from 0% to 100%, and how do I avoid a php timeout?
One possibility would be to process everything in chunks of N items, and letting the script redirect to itself at the end starting at the next block. That way the php will output "converting items 1-100..." and after a few secs, refresh (reload itself) showing "converting items 101-200..." etc.
But is there a better way to do this? It doesn't have to be a fancy page with a graphical progress bar or whatever, just printing "0%...1%...2%...etc" and making sure it doesn't timeout is all I need. I tried using ob_flush() but that didn't seem to do the trick. Or should I combine it with some special headers to avoid timeout or cache issues?
Thanks in advance for any help!
How do I show the progress, e.g. something running from 0% to 100%, and how do I avoid a php timeout?
One possibility would be to process everything in chunks of N items, and letting the script redirect to itself at the end starting at the next block. That way the php will output "converting items 1-100..." and after a few secs, refresh (reload itself) showing "converting items 101-200..." etc.
But is there a better way to do this? It doesn't have to be a fancy page with a graphical progress bar or whatever, just printing "0%...1%...2%...etc" and making sure it doesn't timeout is all I need. I tried using ob_flush() but that didn't seem to do the trick. Or should I combine it with some special headers to avoid timeout or cache issues?
Thanks in advance for any help!