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:
- Upload image(s) to the web server using XHR with progress bar
- Wait for user to submit his post
- Unlink images he did not end up including in his post
- Call a URL that uploads the remaining images to S3 (and update image URLs in post body when done)
- 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!
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:
The solution requires either
exec()orshell_exec(). It doesn’t really matter which one you use, since all output will be discarded anyway. I choseexec().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');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: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.
Finally, I run this bit of PHP code to execute the other (or ANY) web request in the background:
Works like a charm! Hope this helps.