Multiple Uploading using karik-Widget

Following code works quite fine with just one Upload-File. How to change code in order to upload several files?


 'multiple' => true

obviously is not enough!




<?=

$form->field($model, 'avatar[]')->widget(FileInput::classname(), [

    'options' => ['accept' => 'image/*', 'multiple' => true]])

?>



Furthermore, I present my model




<?php


//Code programmed by Thomas Kipp

//Change it, learn it, do as u please!

///path:/models/


namespace frontend\models;


use Yii;

use yii\base\Model;


class myScriptForm extends Model{ // A new Class programmed by Thomas Kipp 


    public $rating;

    public $username;

    public $password;

    public $email;

    public $zahl_up;

    public $zahl_down;

    public $iteration;

    public $rememberMe;

    public $Urlaubsziel;

    public $Urlaubsziel_database;

    public $Automarke;

    public $Geschlecht;

    public $subject;

    public $body;

    public $fileImage;

    public $avatar;

    public $datetime;


    public function rules() {

    $avatar=array();      

        return [

            ['datetime', 'string'],

            ['rememberMe', 'boolean'],

            ['avatar[]','file'],

            ['fileImage', 'file'],

            ['email', 'email'],

            ['email', 'required'],

            ['subject', 'string'],

            ['body', 'string'],

            ['Urlaubsziel', 'string'],

            ['Urlaubsziel_database', 'string'],

            ['Geschlecht', 'string'],

            ['username', 'required'],

            ['password', 'string'],

            ['zahl_down', 'integer', 'min' => 1, 'max' => 5],

            ['rating', 'integer', 'min' => 1, 'max' => 10],

            ['zahl_down', 'required'],

            ['zahl_up', 'integer', 'min' => 1000, 'max' => 2500],

            ['zahl_up', 'required'],

            ['iteration', 'integer', 'min' => 10, 'max' => 50],

            ['iteration', 'required']];

    }


}


//End of class

?>




If you are using yii\UploadedFile, the controller in question would need something like this:




$model->avatar = UploadedFile::getInstances($model, 'avatar');



Note that Instances in getInstances is plural.

Check this

Yii 2 - FileInput Widget

I changed to getInstances, but unfortunately no effect. Here method of Controller:




    public function actionScript() { //A new method, programmed by Thomas Kipp

        $model = new myScriptForm();

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

            $model->fileImage = UploadedFile::getInstance($model, 'fileImage');

            $model->avatar = UploadedFile::getInstances($model, 'avatar[]');

            if ($model->fileImage) {

                echo "<font size='4'><br><br><br><center>File <font color='red'> "

                . "$model->fileImage<font color='black'> successfully uploaded."

                . "<br>It's available in folder 'uploadedfiles' </font></font color></center>";

                $model->fileImage->saveAs(Yii::getAlias('@uploadedfilesdir/' . $model->fileImage->baseName . '.' . $model->fileImage->extension));

            } else

                echo"<font size='4'><br><br><br><center>No Upload-file selected.<br>"

                . "Nothing moved into folder 'uploadedfiles'</font></center>";

            if ($model->avatar) {

                echo "<font size='4'><br><center>File <font color='red'> "

                . "$model->avatar<font color='black'> successfully uploaded."

                . "<br>It's available in folder 'uploadedfiles' </font></font color></center>";

                $model->avatar->saveAs(Yii::getAlias('@uploadedfilesdir/' . $model->avatar->baseName . '.' . $model->avatar->extension));

            } else

                echo"<font size='4'><br><center>No Upload-file selected.<br>"

                . "Nothing moved into folder 'uploadedfiles' </font></center>";

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

        }

        else {

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

        }

    }


}



This link tells me "Note: for multiple file upload, the attribute name must be appended with [] for PHP to be able to read an array of files. "

So, I did, but it didn’t really help in my case. Just one upload possible…

You can select multiple files at the same time.

You may need to loop through values of $model->avatar in your controller.




$counter = 1;

foreach($model->avatar as $avatar)

  {

  $avatar->saveAs(Yii::getAlias('@uploadedfilesdir/' . $avatar->baseName.$counter.'.' . $avatar->extension));

$counter++;

}



Looping doesn’t help,too!!

Changed controler-code like this,without any effect :=(




public function actionScript() { //A new method, programmed by Thomas Kipp

        $model = new myScriptForm();

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

            $model->fileImage = UploadedFile::getInstance($model, 'fileImage');

            $model->avatar = UploadedFile::getInstances($model, 'avatar');

            if ($model->fileImage) {

                echo "<font size='4'><br><br><br><center>File <font color='red'> "

                . "$model->fileImage<font color='black'> successfully uploaded."

                . "<br>It's available in folder 'uploadedfiles' </font></font color></center>";

                $model->fileImage->saveAs(Yii::getAlias('@uploadedfilesdir/' . $model->fileImage->baseName . '.' . $model->fileImage->extension));

            } else

                echo"<font size='4'><br><br><br><center>No Upload-file selected.<br>"

                . "Nothing moved into folder 'uploadedfiles'</font></center>";

            if ($model->avatar) {

                $counter = 1;

            foreach($model->avatar as $avatar){

                echo "<font size='4'><br><center>File <font color='red'> "

                . "$avatar<font color='black'> successfully uploaded."

                . "<br>It's available in folder 'uploadedfiles' </font></font color></center>";

                $avatar->saveAs(Yii::getAlias('@uploadedfilesdir/' . $avatar->baseName.$counter.'.' . $avatar->extension));

            $counter++;

            }   

            } else

                echo"<font size='4'><br><center>No Upload-file selected.<br>"

                . "Nothing moved into folder 'uploadedfiles' </font></center>";

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

        }

        else {

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

        }

    }


}



I can’t select several files at the same time.Both,by link and by localhost just one file can be selected.After having selected one file and reselecting another, first is gonna disappearing!