Archive for April, 2012

yii multiple file upload

Posted on April 25, 2012. Filed under: Uncategorized | Tags: |

when we want to upload multiple files yii has gave a full functioning widget called “CMultiFileUpload”
but the documentation is no clear …
we can’t find there how to catch up uploaded  files and how to save them a directory we need.

let’s see how we do this..

my example is how to upload my product images to my images directory created in my application path(in protected folder)

in my view file put this code as form field
here i use only 3 fields, name field,  images field and price field.

<!–?                        php $form=$this->beginWidget(‘CActiveForm’, array(
‘htmlOptions’ => array(‘enctype’ => ‘multipart/form-data’),
)); ?>

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

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

<!–?php echo $form->labelEx($model,’name’); ?>
<!–?php echo $form->textField($model,’name’,array(‘size’=>60,’maxlength’=>255)); ?>
<!–?php echo $form->error($model,’name’); ?>

<!–?php echo $form->labelEx($model,’product_images’); ?>
<!–?php $this->widget(‘CMultiFileUpload’,array(
‘denied’=>’type is not allowed’, //message that is displayed when a file type is not allowed
‘duplicate’=>’file appears twice’, //message that is displayed when a file appears twice
)); ?>
<!–?php echo $form->error($model,’product_images’); ?>

<!–?php echo $form->labelEx($model,’price’); ?>
<!–?php echo $form->textField($model,’price’,array(‘size’=>10,’maxlength’=>10)); ?>
<!–?php echo $form->error($model,’price’); ?>

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

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

</div><!– form –>

this  widget send an array of uploaded files
the name of that array we define in ‘name’ element.
we can catch up it in the controller using

CUploadedFile::getInstancesByName() method

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

this is return an array of CUploadedFile instances

we can access each instance through foreach loop

foreach ($primary_product_image as $image => $pic) {

  we can save the current image at any where


Read Full Post | Make a Comment ( None so far )

Hello world!

Posted on April 25, 2012. Filed under: Uncategorized |

Welcome to After you read this, you should delete and write your own post, with a new title above. Or hit Add New on the left (of the admin dashboard) to start a fresh post.

Here are some suggestions for your first post.

  1. You can find new ideas for what to blog about by reading the Daily Post.
  2. Add PressThis to your browser. It creates a new blog post for you about any interesting  page you read on the web.
  3. Make some changes to this page, and then hit preview on the right. You can always preview any post or edit it before you share it to the world.
Read Full Post | Make a Comment ( 1 so far )

Liked it here?
Why not try sites on the blogroll...