Data doesn't get saved into the database table when using Yii CMultiFileUpload

I am trying to implement multi files upload within my application. I was referring to this article and tried out this example, and it works (basically, they are the same). But when I try to implement it within my app for some reason it’s not saving any of the data into the database table.

So, when I upload files into the folder (that work’s just fine) I need filename and sessionID to be saved into the table. Here is my table:


CREATE TABLE IF NOT EXISTS `photos` (

  `id` int(11) NOT NULL AUTO_INCREMENT, 

  `filename` varchar(45) NOT NULL,

  `amount` int(11) DEFAULT NULL,

  `pic_size_id` int(11) DEFAULT NULL,

  `users_sid` varchar(40) NOT NULL

  PRIMARY KEY (`id`), 

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Here is my _form.php file:


<div class="form">


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

    'id'=>'photos-form',

    'enableAjaxValidation'=>false,

    'htmlOptions' => array(

            'multiple' => 'multiple',

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

            )

)); ?>


<?php echo $form->errorSummary($model); ?>


<?php

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

'name'=> 'images', 

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

'duplicate' => 'Duplicate file!', 

'denied'=> 'Invalid file type', 

));

?>


<div class="row buttons">

    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

</div>  


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


</div><!-- form -->

And here is my actionCreate() within PhotosController:


public function actionCreate()

    {

            $model=new Photos;


            $images = array();

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


                if (isset($images) && count($images) > 0) 

                {

                    // go through each uploaded image

                    foreach ($images as $image => $pic) 

                    {

                        if ($pic->saveAs(Yii::getPathOfAlias('webroot').'/upload/'.$pic->name)) 

                        {                              

                            $img_add = new Photos();

                            $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->users_sid = Yii::app()->session->sessionID; // this links your picture model to the main model (like your user, or profile model)

                            if(!$img_add->save()) // save your imagesDONE

                            {

                                var_dump($img_add->getErrors());

                                var_dump($pic->name);                                    

                            }                                

                        }

                    }                 

                }             


            $this->render('create',array(

                    'model'=>$model,

            ));

    }


var_dump($img_add->getErrors())

otuput is: array(1) { ["filename"]=> array(1) { [0]=> string(25) "Filename cannot be blank." } }


var_dump($pic->name)

otuput is: string(37) "windows_metro-wallpaper-1920x1080.jpg", so $pic->name contains filename.

And I really don’t know why it’s not adding anything, when in the same example it works.

Also it’s working when I have single upload, but with slightly different actionCreate() and _form.php of course:

PhotosController.php:


public function actionCreate()

    {

        $model=new Photos;

        if(isset($_POST['Photos']))

        {

            $model->attributes=$_POST['Photos'];

                        $model->filename=CUploadedFile::getInstance($model,'filename');

            if($model->validate())

                        {

                            $model->filename->saveAs(Yii::getPathOfAlias('webroot').'/upload/'.$model->filename);


                            $photo = new Photos;

                            $photo->filename = $model->filename;

                            $photo->users_sid = Yii::app()->session->sessionID;

                            if($photo->save())

                            {

                                $this->redirect(array('view','id'=>$model->id));

                            }

                        }           

        }

        $this->render('create',array(

            'model'=>$model,

        ));

    }

_form.php:


<div class="form">


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

    'id'=>'photos-form',

    'enableAjaxValidation'=>false,

    'htmlOptions' => array('enctype' => 'multipart/form-data')

)); ?>


    <?php echo $form->errorSummary($model); ?>


    <!--<div class="row">

        <?php echo $form->labelEx($model,'filename'); ?>

        <?php echo $form->fileField($model,'filename'); ?>

        <?php echo $form->error($model,'filename'); ?>

    </div> -->


<div class="row buttons">

    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

</div>  


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


</div><!-- form -->

You do not need $images declared as an array. Try without that line and let me know.