Image Upload Functionality In Yii Framework

Hi All

I am trying to upload image in yii framework using below code.

If i selected a image it upload successfully but when validation error comes image is gone.

At the time of update functionality if image is not selected gives the error.

my view

<?php echo CHtml::image(’/payroll/images/profile_images/’.$model->image_path,’’,array(‘class’=>“profileImage”));

				echo &#036;form-&gt;fileField(&#036;model, 'image_path',array('onchange'=&gt;&quot;uploadProfilePicture(this)&quot;));


				echo &#036;form-&gt;error(&#036;model, 'image_path');


				?&gt;

my controller

public function actionCreate()

{


	&#036;model=new Users;


	&#036;model-&gt;gender=&quot;1&quot;;


	&#036;model-&gt;under_probation=&quot;1&quot;;


	&#036;model-&gt;is_active=&quot;1&quot;;


	&#036;model-&gt;image_path=&quot;default.png&quot;;


	&#036;address=new EmployeesAddressDetails;


	&#036;user=UserBase::getUserViewRequirements(10);


	if(isset(&#036;_POST['Users'],&#036;_POST['EmployeesAddressDetails']))


	{


		&#036;model-&gt;attributes=&#036;_POST['Users'];


		&#036;model-&gt;created_at=date('YmdHis');


		&#036;model-&gt;created_by=Yii::app()-&gt;user-&gt;firstName;


		&#036;model-&gt;id_tenant=Yii::app()-&gt;user-&gt;idTenant;


		&#036;model-&gt;image_path=CUploadedFile::getInstance(&#036;model,'image_path');


		&#036;isExist=UserBase::isUserExist(&#036;model-&gt;username);


		if(&#33;(&#036;isExist)){


			if(&#036;model-&gt;save()){


			if(&#036;model-&gt;image_path&#33;=&quot;&quot;){


				&#036;model-&gt;image_path-&gt;saveAs(YiiBase::getPathOfAlias('webroot').'/images/profile_images/'.&#036;model-&gt;image_path);


			}


			&#036;address-&gt;attributes=&#036;_POST['EmployeesAddressDetails'];


			&#036;address-&gt;id_user=&#036;model-&gt;id;


			&#036;address-&gt;save();


			if(isset(&#036;_POST['roles'])){


				UserBase::assignRoles(&#036;_POST['roles'],&#036;model-&gt;id);


			}


			&#036;this-&gt;redirect(array('/basicInfo/index','uid'=&gt;&#036;model-&gt;id));


			}


		}else{


			Yii::app()-&gt;user-&gt;setFlash('error', &quot;User Already Exist Please Try With Different Login Id&quot;);


		}





	}





	&#036;this-&gt;render('/users/basicInfo/create',array(


			'model'=&gt;&#036;model,'address'=&gt;&#036;address,'user'=&gt;&#036;user


	));


}

Below is the script to upload a picture

function uploadProfilePicture(input) {

	   if (input.files &amp;&amp; input.files[0]) {


	       var reader = new FileReader();


	       reader.onload = function (e) {


	           var photo = (e.target || e.srcElement).result;


	           &#036;('.profileImage').attr('src', photo);


	       }


	       reader.readAsDataURL(input.files[0]);


	   }


	}

Hi,

CUploadedFile::getInstance($model,‘image_path’); returns an object and which i believe cannot be added to model directly and saved.

So you should first save and then add just file name to model before saving.

Shiv

Hi…

try following :

                    &#036;model-&gt;photo_path =CUploadedFile::getInstance(&#036;model,'photo_path');


		&#036;valid_photo = &#036;model-&gt;validate();


		


		if(&#036;valid_photo)


		{


			   &#036;ext= substr(strrchr(&#036;model-&gt;photo_path,'.'),1);


			   if(&#036;ext&#33;=null)


			   {				


                             &#036;model-&gt;photo_path-&gt;saveAs(Yii::getPathOfAlias('webroot').'/images/album1/'.&#036;model-&gt;photo_path);


			   }


			   if(&#036;model-&gt;save(false))


			        &#036;this-&gt;redirect(array('admin'));


		}