使用 CUploadedFile 上传文件的正确方法是什么 [英] What is the proper way to upload a file using CUploadedFile

查看:29
本文介绍了使用 CUploadedFile 上传文件的正确方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习教程 http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/ 用于上传文件.我编写了以下代码:

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()
   );
}

但问题是,如果同名文件存在于同一个目录下,这些文件既不会被覆盖,也不会以不同的名称保存.我想要的是新图像说 image.jpg,如果存在同名文件,则重命名为:image_1.jpg

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

有可能吗?请回复.

推荐答案

我在 yii 中写了一个简单上传文件的行为

i have wrote a behavior for simple uploading file in yii

您可以在 github

这篇关于使用 CUploadedFile 上传文件的正确方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆