错误 yii2 上传调用成员函数 saveAs() on null [英] error yii2 upload Call to a member function saveAs() on null

查看:61
本文介绍了错误 yii2 上传调用成员函数 saveAs() on null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的程序员世界,请求你们的帮助,我有错误在 null 上调用成员函数 saveAs()"

I am new programmer world and ask for your help guys, I have error "Call to a member function saveAs() on null"

模型中的这段代码

[['file'],'file'],

  • 控制器中的这段代码

    • this code in controllers

                  $docfileload = $model->docname;
              $model->file = UploadedFile::getinstance($model,'file');
              $model->file->saveAs('uploads/'.$docfileload.'.'.$model->file->extension);
              $model->save();
      
              //save path in db
              $model->docfile = 'uploads/'.$docfileload.'.'.$model->file->extension;
      

    • _form 中的此代码

      this code in _form

          <?= $form->field($model,'file')->fileinput(); ?>
      

      在我使用 getInstanceByName 之后,@bluehipy 告诉我我对图片有一个新的问题

      after I used getInstanceByName like @bluehipy told me I have a new issue look to the pic

      在此处输入图片说明

      推荐答案

      试试这个:

          $model->file=UploadedFile::getInstance($model,'file');  
          $model->pic=$model->file->extension; // pic is a field in database varchar(20)  
          $model->save(false) ;
      
          $ImageName=$model->id;
          if (!file_exists('uploads/'.$ImageName)) {
          mkdir('uploads/'.$ImageName, 0777, true);
              }   
          $model->file->saveAs( 'uploads/'.$ImageName.'/'.$ImageName.'.'.$model-
          >file->extension );
      

      `

      这篇关于错误 yii2 上传调用成员函数 saveAs() on null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆