Yii Framework Forum: CHtml::fileField() with jqrelcopy - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CHtml::fileField() with jqrelcopy

#1 User is offline   Afnan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 18-October 11

Posted 23 January 2012 - 12:02 PM

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']);
0

#2 User is offline   Afnan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 18-October 11

Posted 23 January 2012 - 12:31 PM

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')); ?>

0

#3 User is offline   Anand Neema 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 12-May 12

Posted 16 May 2012 - 08:57 AM

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

do like this
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'Upload-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>

Hope this will helped you :-)
0

#4 User is offline   Roger Alexander 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 25-July 11

Posted 02 June 2012 - 12:24 PM

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

#5 User is offline   hemc 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 87
  • Joined: 09-July 12
  • Location:New Delhi

Posted 19 July 2012 - 07:15 AM

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

use
activeFilefield
Please don't add a reply to say thanks, There is already a +1 button to do same.
0

#6 User is offline   muhoroh 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 23-July 12

Posted 09 December 2012 - 09:25 AM

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?
0

#7 User is offline   PatrickM 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 14-July 12

Posted 11 December 2012 - 09:16 AM

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.yiiframew...ileField-detail
0

#8 User is offline   muhoroh 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 23-July 12

Posted 02 January 2013 - 03:16 AM

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 .
0

#9 User is offline   Maninath 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 09-February 14

Posted 21 May 2014 - 04:07 AM

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.

This post has been edited by Maninath: 21 May 2014 - 04:08 AM

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users