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">

<?php


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

    'id'=>'topic-form',

    'enableAjaxValidation'=>false,

    '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>

   

<?php

$this->widget('ext.jqrelcopy.JQRelcopy',

                 array(

                       'id' => 'copylink',

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

                       ));

$x=0;

?>


    

    <div class="row copy">

        <?php        

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

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

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

    </div>


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

 

 

<div class="row buttons">

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

</div>

<?php

$this->endWidget();

?>

</div>

Controller


 public function actionUploadImages($id)

        {   

            $model=new HotelImages;

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

            {

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

                {

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

                }

            }

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

                '$model'=>$model,

                'hotelId'=>$id,

            ));


        }

        /*Get form data and save to database

         * 

         */        

        private function saveImage($formData,$id)

        {

            if ($formData === null)

                return;


            $result = array();

                $idx=0;

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

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

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

            {

                try{

                    var_dump($formData);

                       $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;

                       $model->image->saveAs($file);

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

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


                       //no id is submitted for new items  

                       //if(!empty($formData['id'][$idx])

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

                }

                catch (Exception $c)

                {

                throw new CException('asd');

                }


                       if(!$model->save())

                           return FALSE;


                           $idx++;

            }

                return true;

        }

Dump of $_POST[‘HotelImages’]


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

array

  'title' => 

    array

      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(

'id'=&gt;'Upload-form',


'enableClientValidation'=&gt;true,


'clientOptions'=&gt;array(


	'validateOnSubmit'=&gt;true,


),


 '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][]’, ‘’); ?>

use

activeFilefield

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: http://www.yiiframework.com/doc/api/1.1/CHtml#fileField-detail

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

$images=CUploadedFile::getInstances($model,‘vcImage’);

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.