Want to add/edit with file upload

Hello,

How do I add file upload to the add/edit page and controller.

If a user chooses an image, I want to upload and store the image name in the database.

If they choose nothing, I want to leave the current image name in the database.

Thanks for the help.

You may want to look at this tutorial which is about uploading image as blob into database. To satisfy your need of leaving currently uploaded file, you may try to add rule for file field as not required, anyhow I haven’t tried it.

Thank you, I will check that out.

This is working great now, but I also need to save the image as a thumb.

What do I need to change?

Thank you…




public function beforeSave()

    {

        if($file=CUploadedFile::getInstance($this,'uploadedFile'))

        {

            $this->cat_image=$file->name;

            //$this->file_type=$file->type;

            //$this->file_size=$file->size;

            //$this->file_content=file_get_contents($file->tempName);

	$file->saveAs('../img/category/'.$file->name);

        }

		

		return parent::beforeSave();

    }



I resolved this using phpThumb. Thought I would share.




public function beforeSave()

    {

		Yii::app()->thumb->setThumbsDirectory(Yii::app()->params['imageThumbPath']);


        if($file=CUploadedFile::getInstance($this,'uploadedFile'))

        {

			$imagePath = Yii::getPathOfAlias('webroot') . Yii::app()->params['imagePath'] . $file->name;

			

            $this->pd_image=$file->name;

            //$this->file_type=$file->type;

            //$this->file_size=$file->size;

            //$this->file_content=file_get_contents($file->tempName);

			$file->saveAs($imagePath);


			Yii::app()->thumb

				->load($imagePath)	

				->resize(75,75)

				->save($file->name);

        }

}