duanfoumi5620 2018-06-11 03:58
浏览 180

在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

  • 写回答

1条回答 默认 最新

  • doupin5667 2018-06-11 04:46
    关注

    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')

    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源