在yii2中上传文件时,我遇到了saveAs()作为null的问题

While uploading files in yii2 I am facing an issue of saveAs() as a null. Please see the code below.

if (Yii::$app->request->post()['flag'] == 4){
$model->photo= UploadedFile::getInstance($model,'photo')
$model->photo->saveAs('uploads/'.$model->photo->baseName.'.'.$model->photo->extension);
$model->save(false);
}

Please give the specific ans as I have used the library of UploadedFile and everything.

and when it stored in db it store as the c://fakepath/name.txt

I suspect you just forgot to define the enctype in your form like this

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>

and that is the reason why it cannot find a file when called:

$model->photo = UploadedFile::getInstance($model,'photo')