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…


<?php

     

     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;

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


		if ($model->save())

		{

		echo $model->cv;

		$model->cv->saveAs('cv/'.$model->cv);

		}

		/*$this->redirect(array('site/index'));*/

		}

	else

		$this->render('index',array('registro'=>$form));

	}


}


?>

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

etc…

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

cv.doc

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.

e.g.:




$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:




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


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

  $model->filename=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




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

{

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

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

    if($image)

        $model->image=$image;

    if($model->save())

    {

        if($image)

            $image->saveAs(Yii::app()->basePath.MenuItem::PATH.'menu_'.$model->id.'.'.$model->image->getExtensionName());

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

    }

}



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