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/