Update profile picture with yii

Hi everybody

i want to create a form to update a user profile picture.

1- save the picture in the server

2- save the path of the picture in the user table in the DB.

This is my form

<div class="form" >


    <?php $form=$this->beginWidget('CActiveForm', array(





    )); ?>

<p class="note">Fields with <span class="required">*</span> are required.

<div class="row" >

                <?php echo $model->getThumbnail(); ?>


		<?php echo $form->labelEx($model,'filename'); ?>


                <?php echo CHtml::activeFileField($model, 'picture'); // see comments below ?>

<div class="row buttons" id="dvPhotoSubmit">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'EditPicture' : 'Save'); ?>





<?php $this->endWidget(); ?>


This is the controller

public function actionEditPicture(){


	  	// Add page breadcrumb and title

		$this->addPageTitle(Yii::t(__CLASS__, 'Edit your profile'));

		$this->addBreadcrumb(Yii::t(__CLASS__, 'Edit Picture'), array('user/editPicture'));


                $model = new User();



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

                   [b] $model->picture->saveAs('/images/user/user.jpg');[/b]

                    $user = User::model()->findByPk(Yii::app()->user->id);

                    $user->picture = '/images/user/unknown-person.jpg';

                        //redirect to success pqge



             $profileUser = User::model()->findByPk(Yii::app()->user->id);	  	

                $this->render('profile_editpicture', array('profileUser'=>$profileUser,'model'=>$model));   


move_uploaded_file(/images/user/unknown-person.jpg): failed to open stream: No such file or directory

I understood that the problem is in the bold line, but i don’t knoy how to resolve it.

thank you for your help

use base url

<?php echo Yii::app()->request->baseUrl; ?>


Same problem

PHP warning

move_uploaded_file(/images/user/unknown-person.jpg): failed to open stream: No such file or directory

Stack Trace


/home/maher/www/chess24com/framework/web/CUploadedFile.php(197): move_uploaded_file("/tmp/phpMuHiba", "/images/user/unknown-person.jpg")


/home/maher/www/chess24com/app/controllers/UserController.php(60): CUploadedFile->saveAs("/images/user/unknown-person.jpg")

55                 $model = new User();

56                 if(isset($_POST["User"])){

57                     $model->attributes=$_POST["User"];

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


60                     $model->picture->saveAs(Yii::app()->request->baseUrl.'/images/user/unknown-person.jpg');

61                     $user = User::model()->findByPk(Yii::app()->user->id);

62                     $user->picture = '/images/user/unknown-person.jpg';

63                         //redirect to success pqge


65 //                    $model->picture = 'test';

Where is your images folder supposed to be?

If it’s in the webroot:

$model->picture->saveAs(Yii::getPathOfAlias('webroot') . '/images/user/user.jpg');

what is ur file name? user.jpg or unknown-person.jpg …i didnt get ur problem…








the Problem is that i have already a profile picture and i want to change it, so i have to upload a picture in the server, and change the user data and put the new path for the profile picture.

and now i have a new error

move_uploaded_file: failed to open stream: Permission denied

u got the error on save ?

You mean SaveAs()

now it works very good, and i can find the file in the server.

thank you for your help.

ok…ok so the error is on disply, right ?

i think the problem is with the path

i used like this in one of my application, to display

'<img src="patients/'.$id.'/'.$dat->photo.'.jpg" width="182" height="169" />'

Yes thank you for your help i already resolve it from your last comment.

i made a stupid written mistake.

now it’s fixed.

thank you again for your help