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've write these code to upload a file and save path in database using bind parameters
I hope these is useful for you.
i have wrote a behavior for simple uploading file in yii
you can see guide and download file on github
I alwais rewrite original name with md5() function. Try this code. All images will have a unique name. This will save your model, and then generate a unique name. And save again the model. Is not very clean but works!