What is the proper way to upload a file using CUpl

2019-04-29 04:48发布

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.

标签: php yii
3条回答
再贱就再见
2楼-- · 2019-04-29 05:11

I've write these code to upload a file and save path in database using bind parameters

$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('Y-m-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 these is useful for you.

查看更多
欢心
3楼-- · 2019-04-29 05:20

i have wrote a behavior for simple uploading file in yii

you can see guide and download file on github

查看更多
疯言疯语
4楼-- · 2019-04-29 05:28

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!

$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();
}
查看更多
登录 后发表回答