2016-09-21 8 views
0

iは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へ コールを()

どうしたの? (詳細テンプレートを使用しています)

ありがとうございました。あなたの代わりに$user$thisを使うべきか、あなたは

$user=$this 

理由を追加する必要があります例えば

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

答えて

0

を機能 を保存するために

間違ったファイルパスに言及しているように見えます
+0

ありがとう、これは働いた! しかし、それは次のエラーを与える: が不明なメソッドの呼び出し:Yiiの\ウ​​ェブ\のにUploadedFile ::セーブ() 私はYiiの自体 使用のYii \ウェブ\のにUploadedFileからベンダー拡張を使用しています。 私の回線を退避する: $ this-> file-> save(); –

+0

私はsaveAs()を使うべきだと思います。 –

+0

私はそれを稼働させることができました。 $ thisから$ userへの最後の行は、最後にユーザー用のレジスタに保存されました。 $ user-> picture = 'uploads /' .$ imageName。 '。'。$ this-> file-> extension; –

0

、あなたはROOTPATHを言及する必要があります単純に$ userは新しいインスタンスであり、フィールドは空です!

+0

です仕事と同じエラー.. –

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, 
     ]); 
    } 

と私のモデルは、私はあなたの行を置き換え、およびルートにアップロードフォルダを作ったが、それでもdidntの

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

    }