Akshay Khetrapal
Akshay Khetrapal

Reputation: 2676

How to display progress bar in PHP exec functions

I am running an external script in PHP using its exec() function. I was looking for various options to create a progress bar. I am able to create a plain rotating loader through AJAX but I couldn't achieve the percentage progress bar. Is there any way to do that?

Upvotes: 2

Views: 7438

Answers (2)

Rafael Beckel
Rafael Beckel

Reputation: 2544

Depending on the program you want to execute, you could use proc_open() instead of exec(), so you can proccess the output, calculate a percentage and throw it back to your ajax script using ob_flush() and flush().

It's not true that you need to wait for the execution to finish before sending any output, but there are some caveats with your server cache configuration and browsers rendering engines, so it is not so reliable.

If you're not using Websockets (the clean and modern option, which can be achieved with PHP using Ratchet or with nodejs using various options), the most reliable way of doing what you want is with polling.

Briefly, instead of calling your script once, you do a first ajax request to init the proccess and then start poking the server again and again to ask the execution status of your script.

For more information, take a look at those answers:

Run process with realtime output in PHP

PHP - Flushing While Loop Data with Ajax

Grab results from a php exec() while the command is still running?

Upvotes: 5

Thais
Thais

Reputation: 150

PHP runs on the server, thus can not achieve this (to my knowledge), here are some answered questions that might be able to help you.

How to show loading status in percentage for ajax response?
Jquery:: Ajax powered progress bar?

Upvotes: 0

Related Questions