Upload Image From Model And Save Path To Database

i have a form like this

what i want to do is after we select the image and confirm, then the image is stored in specified folder

for example: C:/imageFolder/ and then the path is saved to the database…

how can we do it?

i was trying by following http://www.yiiframework.com/wiki/349/how-to-upload-image-photo-and-path-entry-in-database-with-update-functionality/ but it says the image path cannot be blank… ? something wrong with the code?

In your controller try this code for saving image in folder and path in database




public function actionCreate()

{

    $model=new Mastertable;


    // Uncomment the following line if AJAX validation is needed

    // $this->performAjaxValidation($model);


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

    {

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

                        

     $myfile=CUploadedFile::getInstance($model,'image_path');

     if (is_object($myfile) && get_class($myfile)==='CUploadedFile') {

             $model->image_path="path of folder to save image//{$myfile->name}";

                }                                     

     if($model->save())    

     {

         if (is_object($myfile))

         $myfile->saveAs('path of folder'.$myfile->name);

         $this->redirect(array('admin','id'=>$model->id));

  }

                }

		$this->render('create',array(

			'model'=>$model,

		));


	}



Repalce the Mastertable with your Modelname, and change the "path of folder" with path.

Thanks.