Yii Framework Forum: Image Upload Functionality In Yii Framework - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Image Upload Functionality In Yii Framework

#1 User is offline   Nilesh287 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 10-February 13

Posted 12 March 2013 - 06:19 AM

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 $form->fileField($model, 'image_path',array('onchange'=>"uploadProfilePicture(this)"));
echo $form->error($model, 'image_path');
?>
my controller

public function actionCreate()
{
$model=new Users;
$model->gender="1";
$model->under_probation="1";
$model->is_active="1";
$model->image_path="default.png";
$address=new EmployeesAddressDetails;
$user=UserBase::getUserViewRequirements(10);
if(isset($_POST['Users'],$_POST['EmployeesAddressDetails']))
{
$model->attributes=$_POST['Users'];
$model->created_at=date('YmdHis');
$model->created_by=Yii::app()->user->firstName;
$model->id_tenant=Yii::app()->user->idTenant;
$model->image_path=CUploadedFile::getInstance($model,'image_path');
$isExist=UserBase::isUserExist($model->username);
if(!($isExist)){
if($model->save()){
if($model->image_path!=""){
$model->image_path->saveAs(YiiBase::getPathOfAlias('webroot').'/images/profile_images/'.$model->image_path);
}
$address->attributes=$_POST['EmployeesAddressDetails'];
$address->id_user=$model->id;
$address->save();
if(isset($_POST['roles'])){
UserBase::assignRoles($_POST['roles'],$model->id);
}
$this->redirect(array('/basicInfo/index','uid'=>$model->id));
}
}else{
Yii::app()->user->setFlash('error', "User Already Exist Please Try With Different Login Id");
}

}

$this->render('/users/basicInfo/create',array(
'model'=>$model,'address'=>$address,'user'=>$user
));
}
Below is the script to upload a picture
function uploadProfilePicture(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
var photo = (e.target || e.srcElement).result;
$('.profileImage').attr('src', photo);
}
reader.readAsDataURL(input.files[0]);
}
}
0

#2 User is offline   shiv 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 05-November 11
  • Location:India

Posted 05 April 2013 - 06:54 AM

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
0

#3 User is offline   Ravi Bhalodiya 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 61
  • Joined: 27-August 12
  • Location:Gujarat India

Posted 06 April 2013 - 06:08 AM

Hi..
try following :

$model->photo_path =CUploadedFile::getInstance($model,'photo_path');
$valid_photo = $model->validate();

if($valid_photo)
{
$ext= substr(strrchr($model->photo_path,'.'),1);
if($ext!=null)
{
$model->photo_path->saveAs(Yii::getPathOfAlias('webroot').'/images/album1/'.$model->photo_path);
}
if($model->save(false))
$this->redirect(array('admin'));
}
Regards
Ravi Bhalodiya.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users