Image/file Upload In Yii2

To extend this I wanted to ask where the mistake is?

When I use just:


echo $form->field($model, 'image')->fileInput();

everything works properly - the image is uploaded and the old one deleted.

But not when I want to have more control:




echo $form->field($model, 'image')->widget(FileInput::className(), [

    'pluginOptions' => [

        'options'=>[

            'accept'=>'image/*',

            'multiple'=>true

        ],

        'allowedFileExtensions'=>['jpg','gif','jpeg', 'png'],

        'initialPreview'=>[

            Html::img($model->getImageUrl(), ['class'=>'file-preview-image', 'alt'=>$title, 'title'=>$title]),

        ],

        'showCaption' => false,

        'showRemove' => false,

        'showUpload' => false,

        'browseClass' => 'btn btn-grey',

        'browseIcon' => '<i class="fa fa-folder-open" style="font-weight: 600; line-height: 1.3em;"></i> ',

        'browseLabel' =>  'Profilbild'

    ],

])->label(false)



What am I doing wrong?

I had this very same issue, which was solved by me saving the .php file with encoding: "UTF-8 without BOM"

I deleted the file copied the code to sublime and saved it with encoding: "UTF-8" instead of "UTF-8 with BOM".

But nothing changed it still doesnā€™t work. Or isnā€™t it about the view instead of another file? I builded the form with gii where the upload is within.

I am sorry for mistakes.

I would like to get multiple files in controller.

If I use code in view




$form->field($model, 'attachfile[]')->fileInput(['multiple' => true])



I recieve data in my controller:




$_FILES = [

    'Message' => [

        'name' => [

            'attachfile' => [

                0 => 'conf.zip'

                1 => 'src.zip'

            ]

        ]

        'type' => [

            'attachfile' => [

                0 => 'application/octet-stream'

                1 => 'application/octet-stream'

            ]

        ]

        'tmp_name' => [

            'attachfile' => [

                0 => 'C:\\Windows\\Temp\\php13DA.tmp'

                1 => 'C:\\Windows\\Temp\\php13EA.tmp'

            ]

        ]

        'error' => [

            'attachfile' => [

                0 => 0

                1 => 0

            ]

        ]

        'size' => [

            'attachfile' => [

                0 => 90494

                1 => 4969

            ]

        ]

    ]

]



When I try to use kartik\file\FileInput widget




$form

   ->field($model, 'attachfile[]')

   ->widget(

        FileInput::classname(),

        [

            'options'=>[

                'multiple' => 'multiple'

            ],

            'pluginOptions'=>[

                'uploadUrl' => Url::to(['file/upload']),

                'allowedFileExtensions' => Yii::$app->params['message.file.ext'],

                'maxFileCount' => 3,

            ]

        ])



I recived only one file ( in screen form I select two files ):




$_FILES = [

    'Message' => [

        'name' => [

            'attachfile' => [

                0 => 'src.zip'

            ]

        ]

        'type' => [

            'attachfile' => [

                0 => 'application/octet-stream'

            ]

        ]

        'tmp_name' => [

            'attachfile' => [

                0 => 'C:\\Windows\\Temp\\phpB93F.tmp'

            ]

        ]

        'error' => [

            'attachfile' => [

                0 => 0

            ]

        ]

        'size' => [

            'attachfile' => [

                0 => 4969

            ]

        ]

    ]

]



When i try to drag and drop two files I get nothing on server:




$_FILES = [

    'Message' => [

        'name' => [

            'attachfile' => [

                0 => ''

            ]

        ]

        'type' => [

            'attachfile' => [

                0 => ''

            ]

        ]

        'tmp_name' => [

            'attachfile' => [

                0 => ''

            ]

        ]

        'error' => [

            'attachfile' => [

                0 => 4

            ]

        ]

        'size' => [

            'attachfile' => [

                0 => 0

            ]

        ]

    ]

]



What I have to do for kartik\file\FileInput widget to get all selected file in controller?

Hi, Kartik! I was looking for solution of my problem and couldnā€™t find it :(

I am trying to use multiple fileinput with ajax (usin uploadUrl option).

So, I canā€™t make my code to work correctly. All I need is:

  1. to click on "browse"-button and select several photos

or

  1. open some folder and drag&drop this files to the current field.

if 1 - I receiving array of files and itā€™s workā€™s almost correctly except one thing - if I remove some of added files before click on form-submit button I am getting empty array of files.

  1. if I drag and drop files, I am getting an empty array of files in my controller if I click on form-submit button, but if I press Upload button - action in controller getting post with files. But this way is not good for me, I will hide Upload button and gonna use only form-submit button.

So the question is: how can I tie an ajax form with form submit button? Or I only can to use fileinput without ajax drag and drop featureā€¦

Thanks :)

same issue im facing in multiple file upload. and same in drag & drop uploading.

please guide us kartik.

regards,

webin.

I have solved it. ny way thanx all.

Describe your soloution please!

can i get the solution