I’ve been on the hunt for many years for a decent way of uploading multiple files at once via a browser-based upload form. Finally there is one that seems to do a great job, and be fairly easy to install – the JQuery Uploadify plugin.
There’s a couple of ‘gotchas’ that I came across and thought I would share.
Firstly, if you use PHP sessions for your authentication, you’ll find that your PHP script that accepts the upload won’t have access to the current session. This is because Flash gets given a different session ID. One way to get around this problem is to manually send your PHP session id as one of the options, something like:
'scriptData' : {'sessid' : <php echo session_id(); ?>}
Then your script that accepts the file upload would require:
session_id($_REQUEST['sessid']); session_start();
Secondly, I found that for the ‘script’ option, the plugin requires a relative path. I don’t want to have to keep setting paths because they’re different in my development and live environment, so I did this:
var rel_path = location.href.split(/localhost|\.co\.uk|\.com|index\.php/)[1];
$('#uploadify').uploadify({
'script': rel_path + 'upload_script.php'
// other options here...
})
This is a brilliant plugin – a big thank-you to the authors!
Update 09/09/2009: after lots of hair-pulling/teeth-grinding/tea-drinking, the PHP sessions problem just couldn’t be resolved quickly on the live server so I adopted a different approach in the end:
- A singe use ‘upload authentication key’ is generated each time you upload the file, and stored in the database along with the current user
- Uploadify sends this key via the scriptData option
- The script that accepts the file upload ignores PHP sessions, and instead looks in the database for the authentication key to verify the user that sent the file has access.