Downloads

Google Ad

FileUpload Plugin v3.5 -- Multiple File Uploads

Date: Fri, Oct 30th 2009, 17:24 Author: nick Views: 17250 Comments share

CakePHP File Upload Plugin

Info:
Watch:
Get it:
  • Download Now
  • git clone git://github.com/webtechnick/CakePHP-FileUpload-Plugin file_upload
The newest release of File Upload Plugin is now here -- version 3.5 and includes the much asked for multiple file Upload support through the helper and component.

First the Nitty Gritty.... API Changes

There are some API changes that are involved with this new version, but I've done my best to backport all the changes so the plugin will work exactly as before if you aren't interested in multiple file uploads and change nothing in your app.

New Additions

  • FileUpoad::uploadIds array of created Upload ids
  • FileUpoad::finalFiles array of created Upload names
  • FileUpoad::processAllFiles is done automatically, but if you've set FileUpload::automatic to false in a before filter you'll need to use the new processAllFiles() function instead.

Deprecated

  • FileUpload::uploadId is now depreciated, use $this->FileUpload->uploadIds instead.
  • FileUpoad::finalFile is now depreciated, use $this->FileUpload->finalFiles instead.
  • FileUpoad::processFile is now depreciated, use $this->FileUpload->processAllFiles() instead.

Even though uploadId and finalFile are depreciated now, they will still work as expected with only a single file uploaded. However, if you're uploading multiple files, uploadId and finalFile will represent the *last* file in the sequence of files that was uploaded.

Example: Lets say I've uploaded two images -- image-1.jpg and image-2.jpg -- as a multiple-file upload. Using finalFile would only return image-2.jpg (not image-1.jpg). But I can now use finalFiles as an array of files that were uploaded, like so:
  1. echo $this->FileUpload->finalFile; //returns 'image-2.jpg'
  2.  
  3. //But since I uploaded two images I can see them both through the finalFiles array
  4. echo $this->FileUpload->finalFiles[0]; //returns 'image-1.jpg'
  5. echo $this->FileUpload->finalFiles[1]; //returns 'image-2.jpg'

The same is true for uploadId vs uploadIds.

One final API change is the direct access to processFile function. Before you could set $this->FileUpload->automatic = false; in a before filter and later call processFile(); on your own. You can still do that, but it will only process the first file uploaded -- nothing more. To make sure you process all files in the queue, you'll need to use processAllFiles(); instead. An example of that would be:
  1. /*Assuming you have
  2.   $this->FileUpload->automatic = false;
  3.   in a before filter. */
  4. if ($this->FileUpload->hasFile) {
  5.     $this->FileUpload->uploadDir = 'files/sub/dir/1/2/3';
  6.     $this->FileUpload->processAllFiles();
  7. }

Now On to the Cool Bits...

So now we have all the API changes out of the way, let me show you some tricks I've cooked up with the Helper to make your Multiple File Uploading life easier.

I've re-factored the FileUploadHelper::input() so that you can call it multiple times and all the right magic will work for multiple files.

Example:
  1. echo $fileUpload->input();
  2. echo $fileUpload->input();
  3. echo $fileUpload->input();

Simply calling input() three (or any number) times will build you all the necessary form elements you need to upload three (or any number) of files at the same time. Of course all the same options are available to you, as well as any option that is available to the FormHelper::input() function as the helper will pass along the options as needed.

Not to fret non-model users, I didn't forget about you. =) The new multiple file upload works with non-model as well. Again, let the helper do the work for you:

Example:
  1. echo $fileUpload->input(array('model' => false));
  2. echo $fileUpload->input(array('model' => false));
  3. echo $fileUpload->input(array('model' => false));

Again, all the hard work is done for you. For those of you who don't like using helpers (for whatever reason). The new syntax for non-model multiple file upload fields are as followed: data[file][0], data[file][1], data[file][2], etc...

That's It.... Have fun!

I hope you all enjoy the new changes and features. As always, if you like the plugin, find a bug, or have a feature request -- post a comment. =)