To upload multiple files, Yii has given a full functioning widget called “CMultiFileUpload” http://www.yiiframework.com/doc/api/1.1/CMultiFileUpload
but the documentation about this isn't 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 product images to my images directory created in my application path(in protected folder).
My form have many fields but here I consider about only file field called "Product images"(in my view file).
This widget send an array of uploaded files. Also the file count has limited to 5 here by 'max' option.
The widget has many options you can find all here http://www.yiiframework.com/doc/api/1.1/CMultiFileUpload.
The name of that array we define in ‘name’ option.We can use that name to catch files array at the controller.
We can catch it in the controller using
CUploadedFile::getInstancesByName() method
this is return an array of CUploadedFile instances
we can access each instance through foreach loop
That's it.
If you want to resize this image follow this post Edit images using img extention in Yii
but the documentation about this isn't 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 product images to my images directory created in my application path(in protected folder).
My form have many fields but here I consider about only file field called "Product images"(in my view file).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| //The important part is going here.. <div class="row"> <?php echo $form->labelEx($model,'product_images'); ?> <?php $this->widget('CMultiFileUpload',array( 'name'=>'product_images', 'accept'=>'jpg|png', 'max'=>5, 'remove'=>Yii::t('ui','Remove'), '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 'htmlOptions'=>array('size'=>25), )); ?> <?php echo $form->error($model,'product_images'); ?> </div> |
This widget send an array of uploaded files. Also the file count has limited to 5 here by 'max' option.
The widget has many options you can find all here http://www.yiiframework.com/doc/api/1.1/CMultiFileUpload.
The name of that array we define in ‘name’ option.We can use that name to catch files array at the controller.
We can catch it in the controller using
CUploadedFile::getInstancesByName() method
1
| $product_images = CUploadedFile::getInstancesByName('product_images'); |
we can access each instance through foreach loop
1
2
3
4
5
| foreach ($product_images as $image => $pic) { //we can save the current image at any where $pic->saveAs(Yii::getPathOfAlias('application').'/images/'.$pic->name); //This file save to protected/images/ folder } |
If you want to resize this image follow this post Edit images using img extention in Yii