Uploading Multiple Images With Cmultifileupload

I have found many examples regarding this topics but I think it would be very help full other also as I needed.

I tried to make a very simple example for you.

At first create your tables and generate your model using gii.

Database:

CREATE TABLE IF NOT EXISTS gallery (

id int(11) NOT NULL AUTO_INCREMENT,

gallery_name varchar(100) DEFAULT NULL,

description varchar(100) DEFAULT NULL,

PRIMARY KEY (id)

);

CREATE TABLE IF NOT EXISTS gallery_images (

id int(11) NOT NULL AUTO_INCREMENT,

photo_name varchar(100) DEFAULT NULL,

gallery_id int(11) NOT NULL,

PRIMARY KEY (id),

KEY gallery_id (gallery_id)

);

your controller GalleryController

public function actionCreate()

{

$model=new Gallery;

[color=#ff0000] $images = array();[/color]

[color=#ff0000] if(isset($_POST[‘Gallery’]))[/color]

[color=#ff0000] { [/color]

[color=#ff0000] $model->attributes=$_POST[‘Gallery’];[/color]

[color=#ff0000] [/color]

[color=#ff0000] $images = CUploadedFile::getInstancesByName(‘images’);[/color]

[color=#ff0000] [/color]

[color=#ff0000] if (isset($images) && count($images) > 0) [/color]

[color=#ff0000] {[/color]

[color=#ff0000] if(!is_dir(Yii::getPathOfAlias(‘webroot’).’/images/’. $model->gallery_name))[/color]

[color=#ff0000] {[/color]

[color=#ff0000] mkdir(Yii::getPathOfAlias(‘webroot’).’/images/’. $model->gallery_name); [/color]

[color=#ff0000] // the default implementation makes it under 777 permission, which you could possibly change recursively before deployment, but here’s less of a headache in case you don’t[/color]

[color=#ff0000] }[/color]

[color=#ff0000] [/color]

[color=#ff0000] // go through each uploaded image[/color]

[color=#ff0000] foreach ($images as $image => $pic) [/color]

[color=#ff0000] {[/color]

[color=#ff0000] [/color]

[color=#ff0000] if ($pic->saveAs(Yii::getPathOfAlias(‘webroot’).’/images/’.$model->gallery_name.’/’.$pic->name)) [/color]

[color=#ff0000] {[/color]

[color=#ff0000] // add it to the main model now[/color]

[color=#ff0000] [color=#339966]$model->save();[/color][/color]//save your gallery first

[color=#ff0000] $img_add = new GalleryImages();[/color]

[color=#ff0000] $img_add->photo_name = $pic->name; //it might be $img_add->name for you, filename is just what I chose to call it in my model[/color]

[color=#ff0000] $img_add->gallery_id = $model->id; // this links your picture model to the main model (like your user, or profile model)[/color]

[color=#ff0000] //echo $model->id .’ # ‘.$pic->name.’<br />’;[/color]

[color=#ff0000] $img_add->save(); // save your imagesDONE[/color]

[color=#ff0000] }[/color]

[color=#ff0000] else[/color]

[color=#ff0000] {[/color]

[color=#ff0000] echo ‘write your own’;[/color]

[color=#ff0000] }[/color]

}

if($model->save())

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

}

}

$this->render(‘create’,array(

‘model’=>$model,

));

}

your view _form.php

<?php

/* @var $this GalleryController */

/* @var $model Gallery */

/* @var $form CActiveForm */

?>

<div>

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

‘id’=>’gallery-form’,

‘enableAjaxValidation’=>false,

‘action’=>Yii::app()->createAbsoluteUrl(‘/gallery/create’),

‘htmlOptions’ => array( ‘multiple’ => ‘multiple’, ‘enctype’ => ‘multipart/form-data’),

)); ?>

<p>Fields with <span>*</span> are required.</p>

<?php echo $form->errorSummary($model); ?>

<div>

<?php echo $form->labelEx($model,’gallery_name’); ?>

<?php echo $form->textField($model,’gallery_name’,array(‘size’=>60,’maxlength’=>100)); ?>

<?php echo $form->error($model,’gallery_name’); ?>

</div>

<div>

<?php echo $form->labelEx($model,’description’); ?>

<?php echo $form->textField($model,’description’,array(‘size’=>60,’maxlength’=>100)); ?>

<?php echo $form->error($model,’description’); ?>

</div>

<div>

<?php

[color=#ff0000] $this->widget(‘CMultiFileUpload’, array([/color]

[color=#ff0000] ’name’ => ‘images’, [/color]

[color=#ff0000] ’accept’ => ‘jpeg|jpg|gif|png’, // useful for verifying files[/color]

[color=#ff0000] ’duplicate’ => ‘Duplicate file!’, // useful, i think[/color]

[color=#ff0000] ’denied’ => ‘Invalid file type’, // useful, i think[/color]

[color=#ff0000] ));[/color]

?>

</div>

<div>

<?php echo CHtml::submitButton($model->isNewRecord ? ‘Create’ : ‘Save’); ?>

</div>

<?php $this->endWidget(); ?>

</div><!– form –>

I will try to update this post next time with update delete and display also.

Reference link:

http://www.yiiframework.com/wiki/176/uploading-multiple-images-with-cmultifileupload/

Hi, I have tried your example and the images are uploaded successfully. then i want to display the group(list) of images in the view page? how should i display please help me?

useful script. thx.

how it we want to save image data as an array in a record? such as array(image1, image2, image3 etc).

rgds,

maman

==

Selva, have you gotten the solution? I am facing similar problem with you. Thanks before for reply. MH

Hi

Nice work done shah. Its quite descriptive knowledge that you have shared, one just need little attention to understand that coding. Uploading multiple images is always a big challenge for webmasters to deal in short span of time. I was also having these problems with my website (CouponsRain) as it takes too much time to do that but you have solved that issue with your brilliance.

Thanks

i waiting up you for next with update delete and display also.

cheer up…

i have only one row and i can only store 1 value in db help need all to store

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

            {   


    


         foreach (&#036;images as &#036;image=&gt;&#036;pic)


                {   


              //


          if (


                  &#036;pic-&gt;saveAs(Yii::getPathOfAlias('webroot').'/uploads/'.&#036;model-&gt;location_name.'/'.&#036;pic-&gt;name))   { 


             &#036;model-&gt;save();


            


              &#036;model-&gt;image = &#036;pic;


                &#036;model-&gt;types =&#036;pic-&gt;type;


                &#036;model-&gt;size =&#036;pic-&gt;size;


                &#036;model-&gt;setIsNewRecord(true);


                echo &quot;aman&quot;;


                 }


            }


            }


          


                if(&#036;modelaman-&gt;save())


                 if(&#036;model-&gt;save())


            &#036;this-&gt;redirect(array('view','id'=&gt;&#036;model-&gt;location_id)) ;


                     


             }

Can anyone help? I am also with the same difficulty.

Could do as written, now I want to display only the related images.