What is the proper way to upload a file using CUploadedFile

10,832

i have wrote a behavior for simple uploading file in yii

you can see guide and download file on github

Share:
10,832
ajaybc
Author by

ajaybc

Updated on June 11, 2022

Comments

  • ajaybc
    ajaybc almost 2 years

    I am following the tutorial http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/ for uploading a file. I have written the following code :

    $menuitem->attributes = $_POST['MenuItems'];
    $menuitem->clientId = Yii::app()->user->clientId;
    $menuitem->image = CUploadedFile::getInstance($menuitem, 'image');
    if($menuitem->save()){
       $menuitem->image->saveAs(
           Yii::app()->getBasePath()."/../../".$menuitem->image->getName()
       );
    }
    

    But the problem is that if a file with the same name exists on the same directory, the files is neither overwritten or saved with a different name. What I want is the new image say image.jpg, if a file of the same name exists, to be renamed to : image_1.jpg

    Is it possible ? Please reply.