Reputation: 5550
I have implemented a SAAS scenario with my Windows server: a user could upload a file in a website, then Fetch.exe
is an application coded in C#
hosted in the server, Fetch.exe
takes the uploaded file as an input, executes and generates an output to download for the user.
So in my php, I use exec
to wrap Fetch.exe
:
exec("Fetch.exe " . $inputFile . " > " . $outputFile)
Uploading and executing (ie, Fetch.exe
) may take more than several seconds, and I want to show the user that it is processing and everything is going fine.
I have found some threads that discuss how to show a progress bar for the uploading. Whereas, does anyone know what I could do to show the approximate progress of Fetch.exe
? Do I have to split it into smaller applications and use several exec
?
Upvotes: 1
Views: 182
Reputation: 717
Your PHP program needs a way to know the state of the subprocess (the Fetch.exe application), so, Fetch.exe needs to send info about the processing state, the most natural way to do this is through the standard output (the standard output is the information that provides a program when you run it from cmd).
Knowing this, you can run and keep reading a subprocess output from php using popen().
And secod, you can use the PHP ob_flush() and flush() with the onmessage javascript event to establish the comunication from your client page with your running php script, here you can find a good tutorial on how do this.
Upvotes: 1
Reputation: 1384
You could supply Fetch.exe
with an randomly generated ID from php, for example from the uniqueid
function. Fetch.exe will create a file called <uniqueid>.txt
with a progress percentage. From the browser, you could call another script with that unique ID to get the contents of that .txt file. In order, it would be something like this:
The following happens in parallel:
/progress/uniqueid.txt
with the uniqueid as name. It logs the progress into it.http://example.com/progress/uniqueid.txt
and shows the progress to the userAnd finally, when the progress reaches 100% the browser downloads the file. The only thing you might want to add is the pruning of the progress files after a while. Say you delete all files older than 10 minutes every hour.
Upvotes: 2