如何在不同的文件夹中上传相同的文件?

时间:2017-01-04 02:24:08

标签: yii2

我有以下控制器,我想上传相同的文件并保存在不同的文件夹中,但我的第二个saveAs()无效。

$model->profil_picture_file = UploadedFile::getInstance($model,'profil_picture_file');
$model->profil_picture_file->saveAs('path to first folder'); //it works image saved
$model->profil_picture_file->saveAs('path to second folder'); //doesn't work not saved

让我知道我做错了什么?为什么会出错?

提前致谢。

2 个答案:

答案 0 :(得分:2)

像这样使用:

$model->profil_picture_file = UploadedFile::getInstance($model,'profil_picture_file');
$model->profil_picture_file->saveAs('path to first folder', false); //set deleteTempFile  param to false
$model->profil_picture_file->saveAs('path to second folder'); //it will work now and delete temp file

saveAs()接受2个参数 - filedeleteTempFile

文档:Yii2 UploadedFile::saveAs()

答案 1 :(得分:-2)

因为文件saveAs使用移动文件的move_uploaded_file,所以第二个saveAs不起作用,因为文件已经移动了。因此,请使用copy('path to first folder', 'path to second folder')