exec, run until finished

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I call a program through exec that can take a long time to execute.
This is fine but I would like to show some kind of animation or
something while the program is running and when it is finished letting
the user download the file.

Right now I'm using code similar to this simplified example:

----- index.php -----
<form method="post" action="genetate.php" target="_blank">
                <input type="submit" value="Generate!"/>

----- generate.php -----
        $tmpname = time() . md5("bla");

        // Writes to $tmpname
        exec("takesalotoftime $tmpname");

        $len = filesize("$tmpname");

        header('Content-type: application/postscript\r\n');
        header("Content-Length: $len;\r\n");
        header('Content-Disposition: attachment; filename="' . $tmpname
. '"\r\n');


So what is the best way to start the exec, show an animation until it
is finished and then send the file to the user?

Re: exec, run until finished

Andreas.Burman@gmail.com wrote:
Quoted text here. Click to load it

You won't be able to do it in PHP.  Flash would be one possibility.  But  
you also need to have a way for the exec'd job to communicate back to  
the flash program - which won't be easy.

This could be easier if it weren't an exec'd program - then it would be  
using the http connection.  As a batch program, though, this connection  
isn't available to it.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: exec, run until finished

On 1 Jan 2007 11:48:57 -0800, Andreas.Burman@gmail.com wrote:

Quoted text here. Click to load it

 One approach; show a page with an animated gif, flush the page, run the
long-running exec, then do a Javascript redirection when it's done (bearing in
mind that it's not 100% reliable).

 Or move it back a step; redirect to page a with an animation and a notice to
the user to be patient, redirect to the long-running page and rely on the
browser not updating the page until the next one starts returning stuff.

 If the process can provide feedback on the progress, then proc_open and
HTML_Progress from PEAR may let you get more sophisticated.

 You have a few options since it looks like you're writing to a file so you can
always fetch that on a separate page - although that runs the risk of leaking
temporary files if it's not a true temp file (but if it is a true temp file
then it'd disappear anyway).

 How long does it take? You may run into maximum execution time limits as well
as the users' impatience.

Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

Re: exec, run until finished

Andy Hassall skrev:

Quoted text here. Click to load it

Thanks for all the suggestions but it looks like i'am not going to need
them. The execution took between 30-60 seconds on my test server, but
when I tried it on the real server it took between 1-5 seconds :)

Quoted text here. Click to load it

Re: exec, run until finished

On Mon, 01 Jan 2007 21:22:30 +0000, Andy Hassall wrote:

Quoted text here. Click to load it

In my opinion, long running things are not suitable for web interface. If
you have a batch job, you can put it on some kind of a queue and
communicate results. Having browser waiting for the data from the server
for extended periods of time is an extremely bad practice, application
design-wise. It is practically guaranteed to infuriate users and make them  
demand the designer's head on an aluminum platter. No need to waste silver.


Site Timeline