views:

112

answers:

1

I have a PHP script that takes about 10 minutes to complete.

I want to give the user some feedback as to the completion percent and need some ideas on how to do so.

My idea is to call the php page with jquery and the $.post command.

Is there a way to return information from the PHP script without ending the script?

For example, from my knowledge of this now, if I return the variable, the PHP script will stop running.

My idea is to split the script into multiple PHP files and have the .post run each after a return from the previous is given.

But this still will not give an accurate assessment of time left because each script will be a different size.

Any ideas on a way to do this?

Thanks!

+7  A: 

You can echo and flush() output, but that's suboptimal and rather fragile solution.

For long operations it might be good idea to launch script in the background and store/updte script status in shared location.

e.g. you could lanuch script using fopen('http://… call, proc_open PHP CLI process or even just openg long-running script in an <iframe>.

You could store status in the database or in shared memory (using apc_store()).

This will let user to check status of the script at any time (by refreshing page, or using AJAX) and user won't lose track of the script if browser's connection times out.

It also lets you avoid starting same long script twice.

porneL
thanks! i didn't think of this. I can have it update the database, and then have another .post command that will automatically update the page with the current percent from the database.. lets see what other ideas people have
krio
If you launch a 10-minute script using fopen(), won't it get killed way before the 10-minute mark? Just asking...
MiseryIndex
Long-lived PHP tasks have to execute `set_time_limit(0)` and `ignore_user_abort(true)` to avoid being killed, and then it doesn't matter if they're launched by user or via `fopen('http://…)`.
porneL
Cool, thanks porneL!
MiseryIndex