Yii2 表单中存在多个“文件上传字段”(区别与一个字段上传多个文件) [ 2.0 版本 ]
class UploadForm extends Model
{
/**
* @var UploadedFile
*/
public $img_alipay;
public $img_wxpay;
public function rules()
{
return [
[['img_alipay'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, gif, jpg'],
[['img_wxpay'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, gif, jpg'],
];
}
public function upload()
{
if ($this->validate()) {
if($this->img_alipay){
$this->img_alipay->saveAs(Yii::getAlias('@frontend').'/web/uploads/' . $this->img_alipay->baseName . '.' . $this->img_alipay->extension);
}
if($this->img_wxpay){
$this->img_wxpay->saveAs(Yii::getAlias('@frontend').'/web/uploads/' . $this->img_wxpay->baseName . '.' . $this->img_wxpay->extension);
}
return true;
} else {
return false;
}
}
}
public function actionCreate()
{
$model = new UserLevel();
if ($model->load(Yii::$app->request->post())) {
$modelUpload = new UploadForm();
$modelUpload->img_alipay = UploadedFile::getInstance($model, 'img_alipay');
$modelUpload->img_wxpay = UploadedFile::getInstance($model, 'img_wxpay');
if ($modelUpload->upload()) {
if($modelUpload->img_alipay){
$model->img_alipay = '/uploads/' . $modelUpload->img_alipay;
}
if($modelUpload->img_wxpay){
$model->img_wxpay = '/uploads/' . $modelUpload->img_wxpay;
}
}
if (!$model->save()) {
return $this->redirect('error');
}else{
return $this->redirect(['view', 'id' => $model->id]);
}
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
balx
注册时间:2016-11-22
最后登录:2023-06-06
在线时长:19小时32分
最后登录:2023-06-06
在线时长:19小时32分
- 粉丝3
- 金钱65
- 威望10
- 积分355
共 0 条评论