image file validation fails

I am trying to validate an image file uploaded by users

I have the model:


class Media extends CActiveRecord

{

    public $file;

    

    function rules(){

        return array(

	....

	array('file', 'file',   'types' => 'gif, jpg, jpeg, png', 'allowEmpty' => false),

        ....

     }

     public function beforeValidate(){

		$this->file = CUploadedFile::getInstance( $this, 'file');

		if($this->file!=null){

		     //do stuff

		}

     

          return parent::beforeValidate();

     }

}



the view file has this:


<label class="control-label" for="Media_file">Upload new picture:</label>			

<div class="controls">

	<?php echo $form->fileField( $media, 'file' ); ?>

	<?php echo $form->error( $media, 'file' ); ?>

</div>

but I can not pass the validation, it always output "file can not be blank", even through I do upload a real image, does body has an idea what is going wrong here? thanks for your help.

read wiki: http://www.yiiframework.com/wiki/2/




$model->attributes=$_POST['Media'];

$model->file=CUploadedFile::getInstance($model,'file');

if($model->save()) {

....



Thanks you for sharing

Hi redguy, thanks for the reference and reply, I did upload the file before validate, but it still could not pass the validation(see the updated code).the same error message. it is really wired for me and I seems to have no clue what is going wrong.