Possible Duplicate:
Show progress for PHP long script
I have a PHP script that generates large PDF document on request. This usually takes about 1-2 minutes, and the only way it lets the user know that it is finished is by sending an email when it the whole process completes.
I would like to have some kind of a progress bar displayed to the user while the file is generated. What is the best way to approach this task? The only thing that comes to my mind is that the PDF-generating script should save data in the database (I already use MySQL in the project), and on the client-side I could include Ajax request that would poll for this progress data say every 5 seconds and display it to the user. So probably there would be two Ajax requests: one to launch the PDF-generating script and another one for the user to see the progress.
I think I can do it this way, but is there any standard way that I maybe don’t know of?
Your proposed approach:
…is probably the easiest and it also walks on known grounds so it’s something you’ll handle and debug easily.
To it i can only add that you could use a fork/exec/passthrough instead of the ajax call that initiates the PDF generation.
A more elegant (to be read: complicated) approach:
PHP Gearman is a stand alone server you can run on the machine which has a PHP communication library.
You can have the Gearman daemon running, give it a task to work on and write data to the database for the progress bar. The advantage of this setup is that fork/exec/passthrough are expensive operations – Gearman being a daemon is already on so it handles the exec call to the PHP worker and your triggering script will need to work for a shorter time.
I’m not sure if it has a communication API to ask the Gearman about the state of a certain worker and thus avoid adding a database to the cocktail.