这是我的控制器我很困惑所以让我知道这里的错误是什么
$imgName = $model->uploadimg;
$model->file = UploadedFile::getInstance($model, 'uploadimg');
$model->file->saveAS('uploads/'.$imgName.'.'.$model->file->extension);
$model->uplaodimg='uploads/'.$imgName.'.'.$model->file->extension;
This is view
echo $form->field($model, 'uploadimg')->widget(FileInput::classname(), [
'options' => ['accept' => 'image/*'],
]);
答案 0 :(得分:1)
将Activeform
选项enctype
设置为multipart/form-data
,如下所示:
<?php $form = ActiveForm::begin([
'options' => ['enctype'=>'multipart/form-data']
]); ?>
答案 1 :(得分:0)
查看:
use yii\web\UploadedFile;
<?= $form->field($model, 'uploadimg')->fileInput(['multiple' => true, 'accept' => 'image/']) ?>
控制器:
public function actionCreate()
{
$model = new Model_Name();
if ($model->load(Yii::$app->request->post())) {
$model->uploadimg = UploadedFile::getInstance($model, 'uploadimg');
$model->save();
}
}