How to make CMultiFileUpload work with CUploadedFile?

Anyone successfully put these two together?

I was trying to figure out a Yii way to handle file upload. The regular single file upload is well taken care of by Form and CFileUpload, however when I try to implement a multiple file upload using CMultiFileUpload and CUploadedFile, things become ugly.

I started with the simplest way, no form required, I have a view like

in controller, I use $_FILES['uploadfile'] to get the uploaded file array, which is fine. however, I couldn't find a way to pass to CUploadFile, which has only getInstance () and getInstanceByName() motheds, none of them seems fit the situation here. ( getInstance () only works with form, while getInstanceByName() seems only works with single upload).

I am not sure if I miss sth there, so any advice is welcome.

PS.: From I what I have learned, I think there is a misleading instruction in the CMultiFileUpload  doc http://www.yiiframew…MultiFileUpload.

Quote:

Quote

This is based on the jQuery Multi File Upload plugin. The uploaded file information can be accessed via $_FILES[widget-id], which gives an array of the uploaded files. Note, you have to set the enclosing form’s ‘enctype’ attribute to be ‘multipart/form-data’.

actually, you can only access uploaded file information via $_FILES['inputfiled_name'], NOT $_FILES[widget-id].

Anyone interested in this topic?

Hi,

try this http://jquery-multif…ml#tab-Examples

This solution not base on CUploadedFile but work :)

Maybe you will be interested on this: http://www.yiiframew…oc/cookbook/29/

I maked an example with multi image upload but you can adapt it on your own scenario :)

thanks for your replies.

@qwerty : If you check the CMultiFileUpload class reference, you will see CMultiFileUpload is bridge class for jqueryMultipleFile plugin.

@StErMi : Though my original attempt is to make  CMultiFileUpload work with CUploadedFile, I gave the cookbook example a try, however it has many typos among other issues, I have left a comment there. Basically, I cannot get the file info in $_POST.

Hi,

you are right @will.

I use CMultiFileUpload and it works fine.

My view file:



...





<div class="form">


<?php echo CHtml::form('','post',array('enctype'=>'multipart/form-data')); ?>





...





<?php $this->widget('CMultiFileUpload',array('name'=>'files', 'htmlOptions'=>array('class'=>'mf'), 'denied'=>Yii::t('mf','Nie można zazanczyć pliku'),'duplicate'=>Yii::t('mf','Ten plik jest już wstawiony'),'remove'=>Yii::t('mf','usuń'),'selected'=>Yii::t('mf','Zaznaczono plik: $file'),)); ?>


Controller file:



...





foreach($_FILES['files']['name'] as $key => $filename)


{


	move_uploaded_file($_FILES['files']['tmp_name'][$key],$path.'/'.$filename);


}





...





I hope this help

Thank you qwerty, your approach has the work done.

However, I am still wondering if CMultiFileUpload can work with CUploadedFile. Maybe I should dig into the Yii core.

I updated the cookbook with some fix anyway :)

If you want to use CUploadedFile, I believe you can. Instead of qwerty’s approach (of using move_uploaded_file),

you can use




$images = CUploadedFile::getInstancesByName('files'); //'files' is the name of the array (see qwerty's example)



or similar approach

in view:


<?php $this->widget('CMultiFileUpload',array('name'=>get_class($fileModel).'[files]',...

in controller:


<?php CUploadedFile::getInstances(get_class($fileModel),'files'); ?>

The class of $imageModel object does not need to have ‘files’ attribute (i think it wouldn’t be of much use, unless you tweak model). I like this approach since files are uploaded inside $_FILES[model_class_here][files] and there is no bare ‘files’ key in $_FILES.

None of these methods worked for me (didn’t tried move_uploaded… because i think it works).

When i try


$images = CUploadedFile::getInstancesByName('files'); //'files' is the name of the array (see qwerty's example)

I have an empty array on $images. My input’s name: “files[]”

I’m willing to make a very detailed post with examples if anyone helps me out!

Tks

May be your form widget does not have ‘enctype’ => ‘multipart/form-data’

View:




<?php


$form = $this->beginWidget('CActiveForm', array(

    'id' => 'shop-items-form',

    'enableAjaxValidation' => false,

    'htmlOptions' => array(

        'enctype' => 'multipart/form-data')

        ));

?>


...


<?php


$this->widget('CMultiFileUpload', array(

    'name' => 'files',

    'accept' => 'jpeg|jpg|gif|png',

    'duplicate' => 'Duplicate file!',

    'denied' => 'Invalid file type',

));

?>


...


<?php $this->endWidget(); ?>

Controller:


$images = CUploadedFile::getInstancesByName('files');

great question thank you :)