I had been using file upload component by Nick Baker for long time and it worked just fine for single file uploads. Recently i required it to upload multiple files and following this i searced around for an updated version of this plugin that could support multiple file uploads. Fortunately i found it here http://www.webtechnick.com/blogs/view/224/FileUpload_Plugin_v3_5_Multiple_File_Uploads.
It worked just fine for the files inputs with following type of names:
etc.
Now because i had been using multiFile plugin with jquery i preferred type of input name rather than naming it like , . I tried uploading having it named as but it did’t work.
The only work around i could find was to call FileUpload.FileUpload plugin on the fly and supply it a modified array of files which this plugin supported. Here is how i did it. I understand that this is a very customized approach but i wanted this and i guess this is the only approach to upload multiple files with such a name as data[user_files][]. I hope Nick Baker considers supporting this kind of file input names in future versions of his so nice and useful plugin.
Here’s how did it.
File input name of my file is user_files and in html it appears like this:
I did’t want to make any change in the plugin files so i hacked it in controller. Someone having experience with CakePHP and FileUpload plugin should figure it out easily. Here’s the controller code:
if(!empty($this->data) ) { //if form is submitted
if(!empty($this->data['UserStudentTask']['task_files'])) { //if file upload input are there
foreach($this->data['UserStudentTask']['task_files'] as $each_file) { //for each uploaded file
$this->data['UserStudentTask'][]['task_files'] = $each_file; //turning it FileUpload plugin way
}
unset($this->data['UserStudentTask']['task_files']); //we already have turned it in FileUpload plugin ways, we donot need it anymore.
App::import('Component', 'FileUpload.FileUpload'); //loading plugin component on the fly
$this->FileUpload = new FileUploadComponent(); //creating object
$this->FileUpload->options['fileModel'] = 'UserStudentTask'; //setting model which we had in the form
$this->FileUpload->options['fileVar'] = 'task_files'; //name of file field
$this->FileUpload->options['allowedTypes'] = array('*'); //allow any filetype
$this->FileUpload->options['uploadDir'] = "student_task_files"; //name of upload directory
$this->FileUpload->initialize($this); //initializing
$this->FileUpload->startup($this); //executing startup
if($this->FileUpload->success) {
pr($this->FileUpload->finalFiles);
//execute other logic
} else {
pr($this->FileUpload->errors);
}
}
}
I hope this helps someone. If you know of a better way please let me know. Imho this is the best file upload plugin for cakephp at the moment.
Possibly Related posts: