How change a name of a file in CUploadedFile

first excuseme because i not speek english i’m from argentina.

Now I have a problem when I upload a a file.

the problem is in a "name" of a file

i use this code…



     class RegistroController extends Controller


	public function actionIndex()


	$model = new Usuarios;

	$form = new CForm('application.views.forms.registro.registroForm',$model);

	if($form->submitted('registrar') && $form->validate())


		$model->attributes = $form->elements;


		if ($model->save())


		echo $model->cv;










in my db y have a colum named cv and in this column appear the filename and the extension

for example curriculum.doc horarios.odt


but when i upload a second file with the same name for example


and then other user upload

cv.doc the second user "replace" the cv.doc file

how can i "rename" the file name "BEFORE" this save in my database??

same like time().filename;

and then save in a database some like 123123123cv.doc and the filename are saved with the identical name.

please :) try to help me

I have run into the same problem. I have tried setting the "name" properly of CUploadedFile but I get Read Only errors.


$model->fileobj->name = Yii::app()->user->id . '-' . $model->fileobj->name;

If anyone has a suggestion please let us know!

I found a small work-around. You have to use the ->saveAs() method of the CUploadedFile object before you call $model->save(). This will save the file even if the model doesn’t save correctly, but if your rules are setup right then this should not be a problem.

Here is a quick example:


if (CUploadedFile::getInstance($model,'filename')) {


  $newfname = Yii::app()->user->id . '-' . $model->filename;

  $model->filename->saveAs(dirname(Yii::app()->request->scriptFile) . '/assets/uploads/avatars/' . $newfname);

  $model->filename = $newfname;


if($model->save()) {


You will probably want to incorporate a timestamp as well as or instead of the user’s id just in case the user uploads two images with the same name.

Late reply, but issue not solved…

The thing is that one can’t rename the ‘name’ property without redeclaring the class (ie. CUploadedFile).

However… one doesn’t really need to change the name of the file. Check out the code below - should solve your problem. Cheers




    $image=CUploadedFile::getInstance($model, 'image');










The issue is not fixed. You’re saving the real image name in the table and another image name for saving to disk.