How can I upload file in a Form?

Hello,

I’am testing Yii framework and really enjoy it, thanks for the job :).

I'am trying to use upload function in form but I can't successfully done it.

First question :

I created a model, and tried to add the parameter enctype multipart/form-data but I can't do it, I searched in documentation, but did not find anything. How can I do it?

Second question :

After that, I used the following sentence :

CHtml::activeFileField($events,'image');

Is that correct?

Third question

In my model I add :

	public function rules()


	{


		return array(


			array('image','file'),


		);


}

Is that correct also?

And finally how can I save the file in the controller?

Many thanks in advance for your answer

  1. Use the following code:


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


  1. That's correct.

  2. Correct. This is mainly to ensure that the file is uploaded and has the required extension.

  3. You may use the following code to save the file:



$file=CUploadedFile::getInstance($events,'image');


$file->saveAs($fileName);


Thank you very much :)