Archive for May 20th, 2007
SWFUpload Component
I have been looking for this kind of component for a while and it just deals almost everything that I wanted for in an uploader component. For more details about this component, you may check James Revillini’s website, the author of the component, and/or the bakery. I also found his kit very useful. The kit is for multiple uploads and I already liked the design. I haven’t had the time to change the layout though. As for the changes that I mentioned, here is the list:
- applied the suggestions of Matthias Willerich, I will also use this component for my non-swfuploader forms so I wanted it to be independent to the structure..
- added mime type validation, I got the idea from Chris Partridge’s File Handler component but now I couldn’t find the link in cakeForge. Basically, it checks if the uploaded file is an accepted one. This is not mandatory so you can leave the checking if you don’t want to. Sample codes below.
- added the function deleteRecentUpload() to be able to easily delete the image once the processing database was not successful.
- added the option to choose the renaming scheme. Sometimes I need to rename the file to random characters. You can set the
var $random_word_lengthto change the number of characters generated. You can choose a partial or full change or random characters by changing thevar $filenameType. Values accepted are default, full or partial. When I say partial, the original filename will have a prefix of random characters. You can have your own renaming scheme too, check thefindRandomFilename().
A snippet from my controller
if ($this->SwfUpload->upload($this->data['Upload']['image'], array(‘img’))) {
$this->data['Upload']['image'] = $this->SwfUpload->filename;
} else {
$this->set(‘error’, $this->SwfUpload->errorMessage);
}
if (!$this->Upload->save($this->data)){
$this->set(‘error’, ‘Unable to complete request.’);
$this->SwfUpload->deleteRecentUpload();
}
The array('img') is the mime type checking.You can leave it null so the component won’t check that part or you may pass as many values as you want.
$this->SwfUpload->deleteRecentUpload() deletes the last image uploaded.
I hope I explained myself better and the component will also be useful to others.
5 comments May 20, 2007