使用 CUploadedFile 上传文件的正确方法是什么 [英] What is the proper way to upload a file using 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屋!