What is the correct way to upload a file using CUploadedFile

I am reading the tutorial http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/ to download the file. I wrote 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 in the same directory, the files are not overwritten or saved with a different name. I want the new image to say image.jpg if a file with the same name exists to be renamed to: image_1.jpg

Is it possible? Answer, please.

+6
source share
3 answers

I wrote a behavior for simple file upload in yii

you can see the manual and download the file on github

+1
source

I alwais rewrites the original name using the 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 the model again. Not very clean but working!

 $menuitem->attributes = $_POST['MenuItems']; $menuitem->clientId = Yii::app()->user->clientId; if ($menuitem->save()) { $imageName = @$_FILES["MenuItems"]["name"]["image"]; $uniqueName = (md5($imageName . mktime() . $menuitem->id)) . '.' . (end(explode('.', $imageName))); $original = Yii::app()->getBasePath() . "/../../" . $uniqueName; $menuitem->image = CUploadedFile::getInstance($menuitem, 'immagine'); $menuitem->image->saveAs($original); $menuitem->image = $uniqueName; $menuitem->save(); } 
0
source

I wrote this code to upload a file and save the path in the database using the bind options

 $model->attributes=$_POST['Form']; $uploadedFile=CUploadedFile::getInstance($model,'resume'); $path = '/protected/upload/'.time().$uploadedFile; if($model->validate()){ $connection=Yii::app()->db; $filePath=Yii::app()->basePath .DIRECTORY_SEPARATOR.'..'.$path; // file upload location $result = $uploadedFile->saveAs($filePath); //upload file // an SQL with placeholders $hobbies=implode(",",$model->hobbies); // comma deliminated string of hobbies $dob =date('Ym-d',strtotime($model->dob)); // convert date $status=1; // status of record $sql='INSERT INTO student (name, dob, email, gender, hobbies, city, resume, message,status, created_date,modified_date) VALUES(:name,:dob,:email,:gender,:hobbies,:city,:resume,:msg,:status,now(),now())'; $command=$connection->createCommand($sql); // replace the placeholder with the actual username value $command->bindParam(":name",$model->name,PDO::PARAM_STR); $command->bindParam(":dob",$dob,PDO::PARAM_STR); $command->bindParam(":email",$model->email,PDO::PARAM_STR); $command->bindParam(":gender",$model->gender,PDO::PARAM_INT); $command->bindParam(":hobbies",$hobbies,PDO::PARAM_STR); $command->bindParam(":city",$model->city,PDO::PARAM_INT); $command->bindParam(":resume",$path,PDO::PARAM_STR); $command->bindParam(":msg",$model->msg,PDO::PARAM_STR); $command->bindParam(":status",$status,PDO::PARAM_INT); $result=$command->execute(); } 

I hope this is helpful to you.

-1
source

Source: https://habr.com/ru/post/917296/


All Articles