2017-05-31 2 views
0

Also ich arbeite mit Yii2 und bin ziemlich neu dazu. Ich verwende Kartik File Upload und habe versucht, den Code für mehrere Dateien zu konvertieren. aber es speichert nur die erste Datei.Yii2 Kartei Datei FileInput Multiple

Ich habe die Validierung entfernt, da dies auch fehlgeschlagen ist, aber wieder hinzufügen wird, sobald ich weiß, dass alles andere funktioniert.

Modell:

/** 
    * Process upload of image 
    * 
    * @return mixed the uploaded image instance 
    */ 
    public function uploadImage() { 

     // get the uploaded file instance. for multiple file uploads 
     // the following data will return an array (you may need to use 
     // getInstances method) 
     $image = UploadedFile::getInstances($this, 'image'); 

     foreach ($image as $images) { 

     // if no image was uploaded abort the upload 
     if (empty($images)) { 

      return false; 
     } 
     // store the source file name 
     $this->image_src_filename = $images->name; 
     $extvar = (explode(".", $images->name)); 
     $ext = end($extvar); 


     // generate a unique file name 
     $this->image_web_filename = Yii::$app->security->generateRandomString().".{$ext}"; 

     // the uploaded image instance 

     return $images; 


} } 

Controller:

/** 
    * Creates a new PhotoPhotos model. 
    * If creation is successful, the browser will be redirected to the 'view' page. 
    * @return mixed 
    */ 
    public function actionCreate() 
    { 
     $model = new PhotoPhotos(); 

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

      // process uploaded image file instance 
      $images = $model->uploadImage(); 



     if ($model->save(false)) { 

      // upload only if valid uploaded file instance found 
       if ($images !== false) { 

        $path = $model->getImageFile(); 
        $images->saveAs($path); 

       } 

      return $this->redirect(['view', 'id' => $model->ID]); 
     } else { 
      //error in saving 

     } 

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

Ausblick:

//uncomment for multiple file upload 
echo $form->field($model, 'image[]')->widget(FileInput::classname(), [ 
    'options'=>['accept'=>'image/*', 'multiple'=>true], 
]); 
+0

Wer in der Lage zu helfen, sein sollte? Vielen Dank – abilham

Antwort

0

Ich sehe ein Problem, das ist, dass Sie in

$ Bilder und $ Bild umgekehrt
foreach ($image as $images) 

die

foreach ($images as $image) 

Prost

Verwandte Themen