yii2 文件上传错误- 在 null 上调用成员函数 saveAs()

yii2 file upload error- Call to a member function saveAs() on null

我了解了一点yii2框架,但是我遇到了一个无法解决的错误。我正在尝试将图片添加到注册表单。

查看:

            <?php $form = ActiveForm::begin(['id' => 'form-signup', 'options' => ['enctype' => 'multipart/form-data']]); ?>
        
            <?= $form->field($model, 'voornaam')->textInput(['autofocus' => true]) ?>
            <?= $form->field($model, 'bedrijf') ?>
            <?= $form->field($model, 'telefoon') ?>
            <?= $form->field($model, 'username')?>
            <?= $form->field($model, 'email') ?>
            <?= $form->field($model, 'password')->passwordInput() ?>
            <?= $form->field($model, 'file')->fileInput() ?>

            <div class="form-group">
                <?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>
            </div>

        <?php ActiveForm::end(); ?>

控制器

public function actionSignup()
{
    $model = new SignupForm();
    if ($model->load(Yii::$app->request->post())) {
        if ($user = $model->signup()) {
            if (Yii::$app->getUser()->login($user)) {
                return $this->goHome();
            }
        }
    }

    return $this->render('signup', [
        'model' => $model,
    ]);
}

型号:

public function signup()
{
    if (!$this->validate()) {
        return null;
    }
    
    $user = new User();
    
            $imageName = $user->username;
            $user->file = UploadedFile::getInstance($user,'file');
            $user->file->saveAs( 'uploads/'.$imageName.'.'.$model->file->extension );
            $user->picture =  'uploads/'.$imageName.'.'.$model->file->extension; 
            
    $user->voornaam = $this->voornaam;
    $user->bedrijf = $this->bedrijf;
    $user->telefoon = $this->telefoon;
    $user->username = $this->username;
    $user->email = $this->email;
    $user->setPassword($this->password);
    $user->generateAuthKey();
    
    return $user->save() ? $user : null;
}

我遇到错误:

Call to a member function saveAs() on null

我做错了什么? (我使用的是高级模板)。

你好像提到了错误的文件路径,你必须提到根路径才能实现保存功能 例如

   $user->file->saveAs(\Yii::getAlias('@webroot').'/uploads/'.$imageName.'.'.$model->file->extension );

你应该使用 $this 而不是 $user 或者你应该添加

$user=$this

因为 $user 只是新实例,它的字段是空的!

我使用单独的上传功能上传了一个文件,它成功了。

我的观点

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

            <?= $form->field($model, 'username') ?>

            <?= $form->field($model, 'email') ?>

            <?= $form->field($model, 'password')->passwordInput() ?>

            <?= $form->field($model, "files")->fileInput() ?>

            <div class="form-group">
                <?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>
            </div>

        <?php ActiveForm::end(); ?>

控制器

public function actionSignup() {
        $model = new SignupForm();
        if ($model->load(Yii::$app->request->post())) {
            $model->files = \yii\web\UploadedFile::getInstance($model,'files');

            if ($model->upload() && $user = $model->signup()) {
                if (Yii::$app->getUser()->login($user)) {
                    return $this->goHome();
                }
            }
        }

        return $this->render('signup', [
                    'model' => $model,
        ]);
    }

我的模特是

//declare variable
public $files; 

//add this to your rules
[['files'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg, jpeg']


//File upload function
public function upload()
    {
        if ($this->validate()) {
            $this->files->saveAs('../web/uploads/' . $this->files->baseName. '.' .$this->files->extension);
            $this->picture = '../web/uploads/' . $this->files->baseName. '.' .$this->files->extension;
            return true;
        } else {
            return false; 
        }
    }


    public function signup()
    {
            $user = new User();
            $user->username = $this->username;
            $user->email = $this->email;  
            $user->picture = $this->picture;
            $user->setPassword($this->password);
            $user->generateAuthKey();
            if ($user->save()) {
                return $user;
            }
            else{
                return null;
            }

    }

只是分享我的解决方案,将上传文件夹的路径更改为../uploads/

在您的模型中尝试更改 saveAs 函数,如下所示

$user->file->saveAs( '../uploads/'.$imageName.'.'.$model->file->extension );

我是 yii2 的初学者,我在尝试文件上传代码时遇到了问题 https://www.yiiframework.com/doc/guide/2.0/en/input-file-upload,

这解决了我的问题