Foreach loop won't be entered

Hi guys, i use similar code in two yii-projects. The one works pretty fine, the other doesn’t. Foreach-Loop won’t be entered,

although method will. Any ideas,how to fix this?

My model:




<?php

namespace app\models;


use Yii;

use yii\base\Model;

use kartik\widgets\Growl;


class myScriptForm extends Model {




    public $fileImage;


    public function rules() {

        return [

            [['fileImage'], 'file', 'skipOnEmpty' => false, 'maxFiles' => 3],

            ['fileImage', 'required'],

        ];

    }


    public function attributeLabels() {


        return ['fileImage' => 'Image'];

    }


    public function upload() {

        echo Growl::widget([ //This output will be shown

                'type' => Growl::TYPE_SUCCESS,

                'title' => 'Well done!',

                'icon' => 'glyphicon glyphicon-ok-sign',

                'body' => 'File(s) successfully uploaded<br> It is available in folder uploadedfiles',

                'showSeparator' => true,

                'delay' => false,

                'pluginOptions' => [

                    'showProgressbar' => true,

                    'placement' => [

                        'from' => 'top',

                        'align' => 'center',

                    ]

                ]

            ]);

        foreach ($this->fileImage as $uploaded_file) {

            echo Growl::widget([ //This output will not be shown -WHY??

                'type' => Growl::TYPE_SUCCESS,

                'title' => 'Well done!',

                'icon' => 'glyphicon glyphicon-ok-sign',

                'body' => 'File(s) successfully uploaded<br> It is available in folder uploadedfiles',

                'showSeparator' => true,

                'delay' => false,

                'pluginOptions' => [

                    'showProgressbar' => true,

                    'placement' => [

                        'from' => 'top',

                        'align' => 'center',

                    ]

                ]

            ]);

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

        }

        return true;

    }


}


//End of class

?>



My Controller:




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

        try {

            $model = new myScriptForm();

        } catch (InvalidParamException $error) {

            throw new BadRequestHttpException($error->getMessage());

        }

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

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

            if ($model->upload()) {

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

            }

        }

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

    }



And my View:




<?= $form->field($model, 'fileImage[]')->fileInput(['multiple' => true,])->label('Uploadfile(s)') ?>



Did you check if the files are uploaded?

just a dirty


var_dump(UploadedFile::getInstances($model, 'fileImage'));

die();

will give you the answer

Eventually check how did you set the form, it should be multipart (enctype=“multipart/form-data”) otherwise the files won’t be uploaded

There are few suggestion to your implementation


if ($model->upload())

is not necessary (unlless you plan to return false in certain cases) since your upload() method always return true

  • generally speaking a model should save only 1 item, you save multiple files
  • you are echoing in the model instead of the view, save messages either in session or as flash messages and echo the growl in the view
  • echoing the growl in model cause it to be at begin of the page (before <html> tag) since it is fired before the template loads, I’m quite surprised you see something

I got it with this method…




    public function upload() {

        if ($this->validate()) {

            foreach ($this->fileImage as $uploaded_file) {

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

            }

            return true;

        } else

            return false;

    }



and this view…




<?php

$form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]);

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

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

    'pluginOptions' => ['previewFileType' => 'image']

]);

?>