How can I upload file in a Form?


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 :


Is that correct?

Third question

In my model I add :

	public function rules()


		return array(




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:



Thank you very much :)