2016-09-21 174 views
0

我瞭解yii2框架一點點,但我得到一個錯誤至極我並不能解決..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; 
} 

我發現了錯誤:上的空

呼叫一個成員函數的saveAs()

我做錯了什麼? (我正在使用高級模板)。

謝謝。

回答

0

你應該使用$this,而不是$user或你應該添加

$user=$this 

因爲簡單的$ user是新的實例,它的字段是空的!

+0

謝謝,這工作! 但它給了一個錯誤: 調用未知的方法:警予\網絡\ UploadedFile的::保存() 我使用的是從警予本身 使用警予\網絡\ UploadedFile的供應商擴展; 我的保存行: $ this-> file-> save(); –

+0

我想你應該使用saveAs(); –

+0

我設法讓它工作, 我所做的是,最後一行從$ this到$ user,最後它爲用戶保存了註冊表。 $ user-> picture ='uploads /'.$ imageName。'。'。$ this-> file-> extension; –

0

它看起來像你提錯了文件路徑,就不能不提ROOTPATH的保存功能 例如

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

我取代了符合你的,在根製成的上傳文件夾,但仍然didnt工作和相同的錯誤.. –

0

我使用單獨的上傳功能上傳文件,它的工作。

我的視圖

<?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; 
      } 

    }