CMultiFileUpload generates a file input that can allow uploading multiple files at a time. This is based on the jQuery Multi File Upload plugin. The uploaded file information can be accessed via $_FILES[widget-name], which gives an array of the uploaded

Today I will show you how to handle multiple file upload step by step.

In your controller.

public function actionCreate()
        $model = new Photo;
        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        $type = isset($_GET['type']) ? $_GET['type'] : 'post';
        if (isset($_POST['Photo'])) {
            $model->attributes = $_POST['Photo'];
            $photos = CUploadedFile::getInstancesByName('photos');
            // proceed if the images have been set
            if (isset($photos) && count($photos) > 0) {
                // go through each uploaded image
                foreach ($photos as $image => $pic) {
                    echo $pic->name.'<br />';
                    if ($pic->saveAs(Yii::getPathOfAlias('webroot').'/photos/path/'.$pic->name)) {
                        // add it to the main model now
                        $img_add = new Photo();
                        $img_add->filename = $pic->name; //it might be $img_add->name for you, filename is just what I chose to call it in my model
                        $img_add->topic_id = $model->id; // this links your picture model to the main model (like your user, or profile model)
                        $img_add->save(); // DONE
                        echo 'Cannot upload!';
            if ($model->save())
                $this->redirect(array('update', 'id' => $model->id));
        $this->render('create', array(
            'model' => $model,

In your view:

  $this->widget('CMultiFileUpload', array(
        // 'onFileSelect'=>'function(e, v, m){ alert("onFileSelect - "+v) }',
        // 'afterFileSelect'=>'function(e, v, m){ alert("afterFileSelect - "+v) }',
        // 'onFileAppend'=>'function(e, v, m){ alert("onFileAppend - "+v) }',
        // 'afterFileAppend'=>'function(e, v, m){ alert("afterFileAppend - "+v) }',
        // 'onFileRemove'=>'function(e, v, m){ alert("onFileRemove - "+v) }',
        // 'afterFileRemove'=>'function(e, v, m){ alert("afterFileRemove - "+v) }',
     'denied'=>'File is not allowed',
     'max'=>10, // max 10 files

Example #2 for multiple files uploader

Step 1 : Model

Add Rule for File Upload Validation . Here we only allow user to upload documents with the File Extention .doc, .docx, .pdf and .txt. The same you customize for Photo or Image Upload.

array('attachments', 'file', 
'maxSize'=>1024 * 1024 * 1, // 1MB
'tooLarge'=>'The file was larger than 1MB. Please upload a smaller file.',

Step 2 : Controller

Controller handle main File Uploading process. we use CUploadedFile to upload multiple instances of file.

   $model->attachments=implode(",", $filez);

Place the above Php Code in your Action Function and then add the below function any where in your controller.

public function uploadMultifile ($model,$attr,$path)
 * path when uploads folder is on site root.
 * $path='/uploads/doc/'
if($sfile=CUploadedFile::getInstances($model, $attr)){
  foreach ($sfile as $i=>$file){  
     $file->saveAs(Yii::app()->basePath .DIRECTORY_SEPARATOR.'..'. $path.$formatName);
    return ($ffile);

Step 3 : View

In View we use CMultiFileUpload Widget to Upload Files.

<div class="row">
<?php echo $form->labelEx($model,'attachments'); ?>
<?php  $this->widget('CMultiFileUpload',
       'attribute' => 'attachments',
       'accept'=> 'doc|docx|pdf|txt',
       'denied'=>'Only doc,docx,pdf and txt are allowed', 
       'duplicate'=>'Already Selected',
<?php echo $form->error($model,'attachments'); ?>
<div class="hint">You can upload up to four attachments. </div>