Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way to upload a file using CUploadedFile

Tags:

php

yii

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.

like image 667
ajaybc Avatar asked Jun 03 '12 13:06

ajaybc


1 Answers

i have wrote a behavior for simple uploading file in yii

you can see guide and download file on github

like image 113
na3r Avatar answered Oct 05 '22 08:10

na3r