Yii Framework Forum: Image Upload - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Image Upload Image Upload Rate Topic: -----

#1 User is offline   beatcode 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 13-May 11
  • Location:Zürich

Posted 21 March 2017 - 12:28 PM

Hallo Zusammen

Ich habe ein Formular mit 6 Bild Uploads. Der Benutzer soll jederzeit eines der Bilder aktualisieren können. Ich bring das nicht fertig. Es überschreibt mir immer die anderen Bilder wieder mit Null. Wie kann ich prüfen ob der Benutzer ein Feld (Upload) geändert hat?

Vielen Dank
Beat


View: (benutze yii2-widget-fileinput https://github.com/k...idget-fileinput)
<?=  $form->field($model, 'logo')->widget(FileInput::classname(), [
            'options' => ['accept' => 'images/upload/*'],
            'pluginOptions' => [
              'initialPreview'=>[
                Html::img('images/upload/'. $model->logo)
              ],
            ]
          ]); ?>
          
          <?=  $form->field($model, 'praxis1')->widget(FileInput::classname(), [
            'options' => ['accept' => 'images/upload/*'],
            'pluginOptions' => [
              'initialPreview'=>[
                Html::img('images/upload/'. $model->praxis1)
              ],
            ]
          ]); ?>


Controller:
  public function actionUpdate()
  {

    $model = $this->findbyuser(Yii::$app->user->identity->id);

    if ($model->load(Yii::$app->request->post())  ) {

      $model->upload('logo');
      $model->upload('praxis1');
      $model->upload('praxis2');
      $model->upload('praxis3');
      $model->upload('team1');
      $model->upload('team2');

      $model->save();

        return $this->redirect(['view', 'id' => $model->id]);
      } else {
        return $this->render('wizard', [
          'model' => $model,
        ]);
      }
    }



Model: Hier müsste ich irgendwie erkennen ob der Benutzer ein Bild geändert hat ansonsten ist keine Aktion notwendig.
  public function upload($field) {

    $image = UploadedFile::getInstance($this, $field);

    if(!empty($image)){

      $rnd = rand(0,9999);
      $logo_name = Yii::$app->user->identity->id . '_' . $field .'_' . $rnd . '.' . $image->extension;

      Yii::$app->params['uploadPath'] = Yii::$app->basePath . '/web/images/upload/';
      $path = Yii::$app->params['uploadPath'] . $logo_name;
      $image->saveAs($path);
      $this->$field = $logo_name;

    }
  }

0

#2 User is offline   beatcode 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 13-May 11
  • Location:Zürich

Posted 22 March 2017 - 06:43 AM

Mit der Übergabe des alten Wertes funktioniert es. Das geht jedoch bestimmt viel eleganter?

Controller:

  public $old_logo = "";
  public $old_praxis1 = "";


public function actionUpdate()
  {

    $model = $this->findbyuser(Yii::$app->user->identity->id);

    $old_logo = $model->logo;
    $old_praxis1 = $model->praxis1;


    if ($model->load(Yii::$app->request->post())  ) {

      $request = Yii::$app->request;

      $model->upload('logo', $old_logo);
      $model->upload('praxis1', $old_praxis1);

      $model->save();

      return $this->redirect(['view', 'id' => $model->id]);
    } else {
      return $this->render('wizard', [
        'model' => $model,
      ]);
    }
  }



Model:
  public function upload($field, $oldfile) {

      $image = UploadedFile::getInstance($this, $field);

      if(isset($image)){

        $rnd = rand(0,9999);
        $logo_name  = Yii::$app->user->identity->id . '_' . $field .'_' . $rnd . '.' . $image->extension;

        Yii::$app->params['uploadPath'] = Yii::$app->basePath . '/web/images/upload/';
        $path = Yii::$app->params['uploadPath'] . $logo_name;
        $image->saveAs($path);
        $this->$field = $logo_name;

      } else {
        $this->$field = $oldfile;
      }
  }

0

#3 Guest_RichardGhval_*

  • Group: Guests

Posted 28 April 2017 - 12:56 PM

I added a feature so you can do multiple image uploads. Its still cloogy dank, but its better than it was. Ill still try to figure something better out.

To add more uploads in one shot, just hit the symbol next to the one upload. It will add as many as you like. You still have to search for each image one by one though.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users