2

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?

1
  • PHP executes on the server and will not return any values before it is finished. Commented Apr 3, 2015 at 17:56

2 Answers 2

5

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?

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

Comments

0

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?

Comments

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.