Multiple File Upload [ tip ]

HI all. when I read into cookbook. I found http://www.yiiframework.com/doc/cookbook/29/

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();

        if(isset($_POST['Upload']))

        {

            $upload->attributes=$_POST['Upload'];

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

			$upload->backimages->saveAs('./images/upload/'.$upload->backimages);

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

		

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

           if($upload->save())

            {

				$upload->backimages->saveAs('./images/upload/'.$upload->backimages);

				$upload->endimages->saveAs('./images/upload/'.$upload->endimages);

				$this->redirect(array('upload/success'));			

            } else {

				$this->redirect(array('upload/false'));

			}

        }


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

http://www.yiiframework.com/doc/api/CFileValidator figured out