CHtml::fileField() with jqrelcopy

Hi there I am using this plugin to get files uploading but error is that file name is not being sent what i think because in controller it says index ‘image’ is not defined

here is view file

<div class="form">


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



    'action' => Yii::app()->createUrl('hotel/UploadImages',array('id'=>$hotelId)),

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


echo '<h1>Please select Images for hotel. (jpeg,jpg,gif,png)</h1>';



    <div id="asd"></div>





                       'id' => 'copylink',

                       'removeText' => 'remove' //uncomment to add remove link





    <div class="row copy">


        echo CHtml::label('Image Title',''); ?>

        <?php echo CHtml::textField('HotelImages[title][]',''); ?>

        <?php echo CHtml::FileField('HotelImages[image][]', ''); ?>


   <a id="copylink" href="#" rel=".copy">Add another Image</a>    



<div class="row buttons">

	<?php echo CHtml::submitButton('Upload' ); ?>







 public function actionUploadImages($id)


            $model=new HotelImages;



                if( $this->saveImage($_POST['HotelImages'],$id) )










        /*Get form data and save to database



        private function saveImage($formData,$id)


            if ($formData === null)


            $result = array();


                //echo CVarDumper::dump ($formData);

                    //You will get 3 arrays in $formData: id, firstname, lastname

            foreach($formData['title'] as $image)




                       $model = new HotelImages;

                       $model->title = $image;

                       $model->hotel_id = $id;

                       $model->image = CUploadedFile::getInstance($model, $formData['image']);

                       $file= dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR . $model->image->name;


                       //The other attributes can be found at the same postion in the formData

                       //$model-> = $formData['lastname'][$idx]; 

                       //no id is submitted for new items  


                         //  $model->id= $formData['id'][$idx]; 


                catch (Exception $c)


                throw new CException('asd');



                           return FALSE;



                return true;


Dump of $_POST[‘HotelImages’]

array ( 'title' => array ( '0' => 'title' ) )


  'title' => 


      0 => string 'title' (length=5)

PROBLEM that there is no Image here and I cannot get image at line

$model->image = CUploadedFile::getInstance($model, $formData[‘image’]);

and the answer is you do not use

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

you use

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

If you want to add the enctype in form for uploading the images then

do like this

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






 'htmlOptions' =&gt; array('enctype' =&gt; 'multipart/form-data'),

)); ?>

Hope this will helped you :slight_smile:

I have de same problem, and I add the lines for the enctype, but it is not working

instead of this line

<?php echo CHtml::FileField(‘HotelImages[image][]’, ‘’); ?>



I have tried all the above but still the file field posts blank . Is there anyone who have succeeded in posting dynamically generated file fields?

Only had a quick look at your code so not sure if this helps. Uploaded files are not stored in $_POST but rather in $_FILES:

That way you can see the files but the problem is that if you try to print $model->image = CUploadedFile::getInstance($model, $formData[‘image’]) so as to get the file details the error "index ‘image’ is not defined " displays thus making it impossible 2 validate and save .

in your view

you need to write like

<?php echo CHtml::activeFileField($model, ‘vcImage[]’, array(‘multiple’ => true)); ?>

and in your controller u need to get files by


here you can select multiple files where as you can see those files by printing $images

keep the $images in foreach loop and you can save images to server.