rename file and update path

Hello everyone,

I have a form where to create a file: Database record + upload file.

my form : view


......

<?php echo $form->textFieldRow($model, 'file_name', array('class' => 'span5', 'maxlength' => 255)); ?>

<?php $currentaction = $this->getAction()->getId(); 


if ($currentaction == 'create') {

    echo $form->fileFieldRow($model, 'uploaded_file', array('class' => 'span5'));

   

}

?>

.....

Controller :


public function actionCreate(){


.......

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

 $model->original_file_name = $model->uploaded_file->getName();

 $model->extension = $model->uploaded_file->getExtensionName();

  $model->file_size = $model->uploaded_file->getSize();


...

}

Model


 protected function afterSave() {

.....

  $this->path = $this->createFilePath(); // The path of the file exemple c:/files/nameFile.doc

}

All this work fine, The file uploaded to the server named whatever we enter in the form and the path is right.

My problem is with UPDATING!

How can I update the the file name, and Automatically, the physical file will be renamed and the database record path field will be updated

Thank you

For anyone who is interested I have found a solution:


 protected function beforeSave(){ 




.......

 $this->oldModel= self::model()->findByPk($this->id);       

   $old_name=$this->oldModel->file_name;

         $new_name=$this->file_name;

   if($old_name!==$new_name){

          $extenion=$this->extension;

         $old_path=$this->path;

        $this->path = E_STORAGE_PATH_FI . '/files/' . $new_name . "." . $extenion;

        rename ( $old_path,  $this->path);

        }


}

Hope the forum become more active though :)