1

I'm building a forum that will allow users to upload images. Images will be stored on my web server temporarily (so I can upload them with a progress bar) before being moved to an S3 bucket. I haven't figured out a brilliant way of doing this, but I think the following makes sense:

  1. Upload image(s) to the web server using XHR with progress bar
  2. Wait for user to submit his post
  3. Unlink images he did not end up including in his post
  4. Call a URL that uploads the remaining images to S3 (and update image URLs in post body when done)
  5. Redirect user to his post in the topic

Now, since step 4 can take a considerable amount of time, I'm looking for a cron like solution, where I can call the S3 upload script in the background and not have the user wait for it to complete.

Ideally, I'm looking for a solution that allows me to request a URL within my framework and pass some image id's in the query, i.e.:

http://mysite.com/utils/move-to-s3/?images=1,2,3

Can I use a cURL for this purpose? Or if it has to be exec(), can I still have it execute a URL (wget?) instead of a PHP script (php-cli)?

Thanks a heap!

3 Answers 3

1

PHP's

  register_shutdown_function()

is your friend [reference].

The shutdown function keeps running, while your script terminated.

Thus, if everything is available, submit the finale page and exit. The the registered shutdown function continues and performs the time-consuming job.

In my case, I prepared a class CShutdownManager, which allows to register several method to be called after script termination. For example, I use CShutdownManager to delete temporary files no longer needed.

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

10 Comments

Never heard of that but it sounds like it's exactly what I need :) Thanks!
Few people ever used this method. And there's more interesting stuff to discover ;-) Don't forget to vote and accept the answer...
Oh, what I can't seem to find in the manual though: will the shutdown function still be executed if I call a header redirect instead of an exit? As said, I need to redirect the user to the topic page after processing his post.
A header( 'Location: http://...' ) does not terminate the running script. To actually send the HTTP reply, you should exit; immediately after the header() call. The shutdown script runs, when you call exit. Thus: Yes.
Don't forget that header() just sets certain HTTP-reply parameters. It does not change the control flow of your PHP-script.
|
0

Try the following statement:

shell_exec(php scriptname);

Comments

0

I found the solution to this problem which I'm happy to share. I actually found it on SO, but it needed some tweaking. Here goes:

  1. The solution requires either exec() or shell_exec(). It doesn't really matter which one you use, since all output will be discarded anyway. I chose exec().

  2. Since I am using MAMP, rather than a system-level PHP install, I needed to point to the PHP binary inside the MAMP package. (This actually made a difference.) I decided to define this path in a constant named PHP_BIN, so I can set a different path for local and live environments. In this case:

    define(PHP_BIN, '/Applications/MAMP/bin/php/php5.3.6/bin/php');

  3. Ideally, I wanted to execute a script inside my web framework instead of some isolated shell script. I wrote a wrapper script that accepts a URL as an argument and named it my_curl.php:

    if(isset($argv[1]))
    {
        $url = $argv[1];
    
        if(preg_match('/^http(s)?:\/\//', $url))
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_exec($ch);
            curl_close($ch);
        }
    }
    
  4. In this SO question I found the way to execute a shell command in the background. This is necessary because I don't want the user to have to wait until it's finished.

  5. Finally, I run this bit of PHP code to execute the other (or ANY) web request in the background:

    exec(PHP_BIN . ' /path/to/my_curl.php http://site.com/url/to/request
    &> /dev/null &');
    

Works like a charm! Hope this helps.

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.