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.
i have wrote a behavior for simple uploading file in yii
you can see guide and download file on github
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With