I’m uploading some files through the POST in iframe wrappers that might take an unusual amount of time to process.
And before I output the result to be captured by an onload event of iframe I want to let the user know somehow that the upload has finished, and now the processing has started.
Otherwise a user might be confused by the long waiting.
I was thinking to display an initial message, and then add another line once done, and then compare the contents of the outputted page in iframe. But this approach never works for some reason.
Example:
print 'processing started';
for ($i=0; $i < 10; $i++) {
echo '...still processing';
sleep(1);
}
echo 'done';
(Expecting to see the ‘processing started’ message, and ‘done’ once it has finished loading, but it just spits it out in one burst after 10 seconds of waiting).
So I wonder how can this be done?
Another thought I had, is to set a redirection header right in the beginning of the page, so once the file was uploaded it would redirect to some other page, where it would do the processing. Then try to capture the redirect and final response.
But that is kind of weird way, it would also need to pass the submitted parameters and move the uploaded files somewhere from the temporary folder, so they will not be deleted once the php script has been outputted.
…So I wonder if there is any better approach of doing what I am trying to do?
When the file has finished uploading, your PHP will get called so that you can (a) put the file where you want it, (b) issue some HTML to the browser in response and (c) in your case, start the processing.
So what I’d do is save the file, and then start the processing as a background job on the server (run a separate PHP script using the command line PHP started via one of the system calls like exec). Give the job a unique ID which you can (a) create a temporary file named according to the ID, (b) pass to the browser somewhere in the HTML to we can query if the file still exists; and (c) when the job finishes, it removes that file, so we know it has completed.
Now the HTML page in response to the upload can say “finished uploading, processing started” and then run some Javascript which polls the server (using Ajax – dead easy in jQuery, not too hard using XMLHttpRequest directly) asking if the job has finished (identifying the job by the query string in the URL of the polled request). The poll script on the server looks to see if the relevant file is still there and if so says “no, still working” or “yes, done” accordingly.
If there is a way for the job to tell how far through it is, e.g. if you are working through the uploaded file in sequence, you could have the job write the progress percentage to the file periodically and return that in response to the poll, and at the client side display that as a progress bar.