Multiple File Upload [ tip ]

HI all. when I read into cookbook. I found

but I can’t run it ( it error in foreach )

and now I can upload Multiple file. it very easy;

View file:

<?php echo CHtml::beginForm('','post',array('enctype'=>'multipart/form-data')); ?>

<?php echo CHtml::activeFileField($upload, 'backimages'); ?> // image 1

<?php echo CHtml::activeFileField($upload, 'endimages'); ?> // image 2 ... and more ... etc

<?php echo CHtml::submitButton('Upload'); ?>

<?php echo CHtml::endForm(); ?>

and my controller:

public function actionIndex(){


		$upload=new Upload();




            $upload->backimages= CUploadedFile::getInstance($upload,'backimages');


			$upload->endimages= CUploadedFile::getInstance($upload,'endimages');


			$upload->uid= Yii::app()->user->id;






            } else {




		$this->render('index', array('upload' => $upload,));		


models file:

class Upload extends CActiveRecord {

	public $image;


	public static function model($className=__CLASS__){

		return parent::model($className);



	public function tableName(){

		return "tt_images";



	public function rules()


        return array(

            array('image', 'file', 'types'=>'jpg, gif, png'),





I think it will help somebody :)

You cannot call saveAs more the once for a single file. It uses retrieve_from_tmp function which will delete the file from the tmp directory when it has been successfully moved.

Thuss a consecutive call will result in an error.

What about validation?

I think it doesn`t working… ‘image’ and ‘backimage’…‘endimage’

Enybody knows how validate array of files? figured out