Multiple file upload

Hi, i use this, but dont work.

My form:


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

	'id'=>'items-form',

	'enableAjaxValidation'=>false,

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

)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


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


	<div class="row">

                <?php $_SESSION['hid']=2; ?>

                <?php $_SESSION['kategoria']=2; ?>

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

		<?php echo $form->hiddenField($model,'hid', array('value'=>$_SESSION['hid'])); ?>

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

	</div>


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

        <?php echo CHtml::activeFileField($model, 'image[]'); ?>        

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

        <?php echo CHtml::activeFileField($model, 'video[]'); ?>


	<div class="row buttons">

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

	</div>


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

My controller:


public function actionCreate()

            {

                $model=new Items;

                

                // Uncomment the following line if AJAX validation is needed

                $this->performAjaxValidation($model);

                

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

                {

                    //var_dump($_FILES); exit;

                    //$model->attributes=$_POST['Items'];

                    $model = $this->myFileHandler($model, array('image', 'video'));

                }

                $this->render('create', array('model'=>$model));

            }


        public function myFileHandler($model, $imgFieldNameArr){

 

          foreach($imgFieldNameArr as $attribute){

            $instance = CUploadedFile::getInstance($model, $attribute);

            

            if($instance){

              $fullImgName = time().'_'.$attribute.'.'.$instance->getExtensionName();   

              if($attribute=='kepek')  ($type = 1 && $tipus = 'kepek');

              if($attribute=='video')  ($type = 2 &&  $tipus = 'video');

              $fullImgSource = Yii::getPathOfAlias('webroot').'/assets/'.$tipus.'/'.$fullImgName;    


              $instance->saveAs($fullImgSource);    

              $model->$attribute->name = $fullImgName;

              $model->$attribute->size = $instance->size;

              $model->$attribute->type = $type;

            }

          }

          return $model; //return model with updated file path

        }





public function myFileHandler($model, $imgFieldNameArr){

          foreach($imgFieldNameArr as $attribute){

                //var_dump($attribute); exit;

                $instances = CUploadedFile::getInstances($model, $attribute);

                //var_dump($instances); exit;

                $i=0;

                foreach ($instances as $instance) {

                    if($instance){

                      $fullImgName = md5(time().$i).'.'.$instance->getExtensionName();   

                      if($attribute=='image')  ($type = 1 && $tipus = 'kepek');

                      if($attribute=='video')  ($type = 2 &&  $tipus = 'videok');

                      $fullImgSource = Yii::getPathOfAlias('webroot').'/assets/'.$tipus.'/'.$fullImgName;    


                      $instance->saveAs($fullImgSource);    

                      $model->name = $fullImgName;

                      $model->size = $instance->size;

                      $model->type = $type;

                    }

                    $i++;

                }

          }

          return $model; //return model with updated file path

        }

This script is upload all file, but dont insert to the table and not validate. :)

Is validate the first element in image and video.


	    public function actionCreate()

            {

                $model=new Items;

                

                // Uncomment the following line if AJAX validation is needed

                //$this->performAjaxValidation($model);

                

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

                {

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

                    //var_dump($model->validate(array('image','video')));

                    if($model->validate(array('image','video'))) {

                        $model = $this->myFileHandler($model, array('image','video') );

                        var_dump($model); exit;

                    }

                }

                $this->render('create', array('model'=>$model));

            }


        public function myFileHandler($model, $imgFieldNameArr){

          foreach($imgFieldNameArr as $attribute){

                $instances = CUploadedFile::getInstances($model, $attribute);

                //var_dump($instances); exit;

                $i=0;

                foreach ($instances as $instance) {

                    if($instance){

                      $fullImgName = md5(time().$i).'.'.$instance->getExtensionName();   

                      if($attribute=='image') { $type = 1; $tipus = 'kepek'; }

                      if($attribute=='video') { $type = 2; $tipus = 'videok'; }

                      $fullImgSource = Yii::getPathOfAlias('webroot').'/assets/'.$tipus.'/'.$fullImgName;    

                      

                      $model->name = $fullImgName;

                      $model->size = $instance->size;

                      $model->type = $type;

                      if($model->save()) {

                          $instance->saveAs($fullImgSource);    

                      }

                    }

                    $i++;

                }

          }

          return true;

        }

It is save the last item to the database and save all file. How to save all data to database?

The final solution is:


          public function myFileHandler($model, $imgFieldNameArr, $items){

          foreach($imgFieldNameArr as $attribute){

                $instances = CUploadedFile::getInstances($model, $attribute);

                //var_dump($instances); exit;

                $i=0;

                foreach ($instances as $instance) {

                    if($instance){

                      $fullImgName = md5(time().$i).'.'.$instance->getExtensionName();   

                      if($attribute=='image') { $type = 1; $tipus = 'kepek'; }

                      if($attribute=='video') { $type = 2; $tipus = 'videok'; }

                      $fullImgSource = Yii::getPathOfAlias('webroot').'/assets/'.$tipus.'/'.$fullImgName;    

                      

                      $model = new Items;

                      $model->attributes=$items;

                      $model->name = $fullImgName;

                      $model->size = $instance->size;

                      $model->type = $type;

                      if($model->save()) {

                          $instance->saveAs($fullImgSource);    

                      }

                    }

                    $i++;

                }

          }

          //exit;

          return true;

        }


            public function actionCreate()

            {

                $model=new Items;

                

                // Uncomment the following line if AJAX validation is needed

                //$this->performAjaxValidation($model);

                

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

                {

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

                    if($model->validate(array('image','video'))) {

                        $items=$_POST['Items'];

                        if($this->myFileHandler($model, array('image','video'), $items)) {

                            $this->redirect(array(Yii::app()->baseUrl));

                        }

                        

                    }

                }

                $this->render('create', array('model'=>$model));

            }