Yii Framework Forum: Uploading Multiple Images With Cmultifileupload - Yii Framework Forum

Jump to content

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

Uploading Multiple Images With Cmultifileupload Using yii CMultiFileUpload widget upload multiple image for gallery Rate Topic: -----

#1 User is offline   Shah Jalal 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 13-June 11
  • Location:Dhaka, Bangladesh

Posted 13 August 2013 - 06:12 AM

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;

$images = array();
if(isset($_POST['Gallery']))
{
$model->attributes=$_POST['Gallery'];

$images = CUploadedFile::getInstancesByName(‘images’);

if (isset($images) && count($images) > 0)
{
if(!is_dir(Yii::getPathOfAlias(‘webroot’).’/images/’. $model->gallery_name))
{
mkdir(Yii::getPathOfAlias(‘webroot’).’/images/’. $model->gallery_name);
// 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
}

// go through each uploaded image
foreach ($images as $image => $pic)
{

if ($pic->saveAs(Yii::getPathOfAlias(‘webroot’).’/images/’.$model->gallery_name.’/’.$pic->name))
{
// add it to the main model now
$model->save();//save your gallery first
$img_add = new GalleryImages();
$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
$img_add->gallery_id = $model->id; // this links your picture model to the main model (like your user, or profile model)
//echo $model->id .’ # ‘.$pic->name.’<br />’;
$img_add->save(); // save your imagesDONE
}
else
{
echo ‘write your own’;
}
}

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
$this->widget(‘CMultiFileUpload’, array(
’name’ => ‘images’,
’accept’ => ‘jpeg|jpg|gif|png’, // useful for verifying files
’duplicate’ => ‘Duplicate file!’, // useful, i think
’denied’ => ‘Invalid file type’, // useful, i think
));
?>
</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.yiiframew...ultifileupload/


1

#2 User is offline   Selvakumar 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 90
  • Joined: 14-August 13

Posted 15 November 2013 - 12:28 AM

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

#3 User is offline   M Hermansyah 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 25-August 13

Posted 04 January 2014 - 05:54 PM

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
0

#4 User is offline   M Hermansyah 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 25-August 13

Posted 06 January 2014 - 07:57 AM

View PostSelvakumar, on 15 November 2013 - 12:28 AM, said:

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?

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

#5 User is offline   bky567 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 10-January 14

Posted 10 January 2014 - 02:51 AM

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
0

#6 User is offline   Liverple 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 16-December 13

Posted 18 January 2014 - 10:31 AM

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

cheer up...
0

#7 User is offline   amanchandel 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 20-March 14

Posted 27 March 2014 - 02:07 AM

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 ($images as $image=>$pic)
{
//
if (
$pic->saveAs(Yii::getPathOfAlias('webroot').'/uploads/'.$model->location_name.'/'.$pic->name)) {
$model->save();

$model->image = $pic;
$model->types =$pic->type;
$model->size =$pic->size;
$model->setIsNewRecord(true);
echo "aman";
}
}
}

if($modelaman->save())
if($model->save())
$this->redirect(array('view','id'=>$model->location_id)) ;

}
0

#8 User is offline   Gustavo Andrade 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 121
  • Joined: 16-August 11
  • Location:Brazil - MG

Posted 01 June 2014 - 10:18 PM

View PostSelvakumar, on 15 November 2013 - 12:28 AM, said:

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?


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

Could do as written, now I want to display only the related images.
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