Problem Using Cmultifileupload For Uploading Files

I’m trying to implement a multiple file upload using CMultiFileUpload with CUploadedFile, but it doesn’t work. Specifically, _POST is not working even considering that I’m using ‘enctype’ => ‘multipart/form-data’ in the options in the view:




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

        'id'=>'examen-form',

        'enableAjaxValidation'=>false,

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

)); ?>



and this is the widget and parameters used for CMultiFileUpload:




        <div class="row">

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

                <?php //echo CHtml::activeFileField($model,'archivo_foto')?>


                <?php $this->widget('CMultiFileUpload', array(

                        'model' => $model,

                        'name' => 'archivo_foto',

                        'accept' => 'jpeg|jpg|gif|png|txt', // useful for verifying files

                        'duplicate' => 'Duplicate file!', // useful, i think

                        'denied' => 'Invalid file type', // useful, i think

                        'max' => 10,

                        'htmlOptions' => array( 'multiple' => 'multiple', 'size' => 25 ),

                )); ?>


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

        </div>




On the other hand, the controller action is implemented this way:




        public function actionUpdateam($id)

        {

                $model=$this->loadModel($id);

                $dir=Yii::getPathOfAlias('application.uploads');

                $model->archivo_documento='funciona 0';

                if(isset($_POST['Examen'])) {


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


                        // THIS is how you capture those uploaded images: remember that in your CMultiFile widget, you set 'name' => 'archivo_foto'

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


                        // proceed if the images have been set

                        $model->archivo_documento='funciona uno';

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

                                $model->archivo_documento='funciona dos';

                                // go through each uploaded image

                                foreach ($images as $image) {

                                        echo $image->name.'<br />';

                                        $image->saveAs($dir.'/'.$image->name);

                                        $model->archivo_foto = $model->archivo_foto."+".$image->name;

                                }

 

                                // save the rest of your information from the form

                                if ($model->save()) {

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

                                }

                        }


                }

 

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

                        'model'=>$model,

                ));

        }



And at last, I think that is important to mention the rule used for the model (it might be the cause of the problem as well):




array('archivo_documento, archivo_foto','file','allowEmpty'=>true,'maxFiles'=>10),



Also, I have followed directions of this page, without any success.

I think that the problem is in post method, because the controller is not uploading the files and is not making any changes in the database.

I have reviewed your code and find a way out for this problem.

In your scenario, the rule of allowEmpty string conflicts with model field validation rules for ‘archivo_foto’ field. So you don’t have to add following rule in your model.


array('archivo_documento, archivo_foto','file','allowEmpty'=>true,'maxFiles'=>10),

If you want to go through your scenario, you need to make changes in your controller as below:


$model->save(false)

Let me know if you face any query/concern regarding this.

Thanks,

Saurabh Dhariwal