Yii Framework Forum: Problem Using Cmultifileupload With Cuploadedfile - Yii Framework Forum

Jump to content

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

Problem Using Cmultifileupload With Cuploadedfile Rate Topic: -----

#1 User is offline   leonias 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 15-February 13

Posted 27 May 2013 - 05:02 AM

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

                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
                        if (isset($images) && count($images) > 0) {
                                // 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 attribute (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 appreciate any help to help me understand this issue. Let me know if you need more info about the problem. thanks in advance.
0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,085
  • Joined: 16-February 11
  • Location:Japan

Posted 27 May 2013 - 10:13 AM

Hi leonias,

I have never used CMultiFileUpload, but in general you have to use 'model' and 'attribute' pair instead of 'name' and 'value' when you want to use a widget in CActiveForm.
                <?php $this->widget('CMultiFileUpload', array(
                        'model' => $model,
                        // 'name' => 'archivo_foto',
                        'attribute' => '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 ),
                )); ?>

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