Can't Upload Image By Using Kartik's Fileinput In Yii2.0

The view:


<?php


use yii\helpers\Html;

use kartik\widgets\ActiveForm;

use kartik\widgets\FileInput;




/**

 * @var yii\web\View $this

 * @var frontend\models\Service $model

 * @var yii\widgets\ActiveForm $form

 */

?>


<div class="row">

    <div class="col-md-9">  

        <?php

        $form = ActiveForm::begin([

                    'id' => 'login-form-horizontal',

                    'type' => ActiveForm::TYPE_HORIZONTAL,

                    'formConfig' => ['labelSpan' => 3, 'deviceSize' => ActiveForm::SIZE_SMALL,

                    'options' => ['enctype' => 'multipart/form-data']]

        ]);

        ?>

        <div class="panel panel-default">

            <div class="panel-body">


                <?= $form->field($model, 'title')->textInput(['maxlength' => 50]) ?>


                <?= $form->field($model, 'category_id')->textInput() ?>


                <?php

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

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

                    'pluginOptions' => [

                        'previewFileType' => 'image',

                        'showUpload' => false

                    ],

                ]);

                ?>

                

                <?= $form->field($model, 'description')->textInput(['maxlength' => 2000]) ?>


                <?= $form->field($model, 'tags')->textInput(['maxlength' => 50]) ?>


                <?= $form->field($model, 'duration')->textInput() ?>


                <?= $form->field($model, 'instruction')->textInput(['maxlength' => 500]) ?>




            </div>

        </div>

        <div class="form-group" style="text-align: center">

            <?= Html::submitButton($model->isNewRecord ? 'save&next' : 'update&next', ['class' => $model->isNewRecord ? 'btn btn-lg btn-success' : 'btn btn-lg btn-primary']) ?>

        </div>

        <?php ActiveForm::end(); ?>

    </div>


</div>




The controller:


    public function actionCreate() {

        if (Yii::$app->user->isGuest) {

            return Yii::$app->user->loginRequired();

        } else {

            $model = new Service;

            $model->user_id = Yii::$app->user->getId();

            $model->created_at = date_timestamp_get(date_create());

            $model->updated_at = date_timestamp_get(date_create());

            

            $file = UploadedFile::getInstance($model, 'gallery');

                

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

                $file->saveAs('/real/path/to/postimages/');

                return $this->redirect(['view', 'id' => $model->id]);

            } else {

                return $this->render('create', [

                            'model' => $model,

                ]);

            }

        }

    }

The error screenshot:

5652

6d535f7419898bbb9d1083be50a32569.png

i don’t know where was wrong, please help

anyboay can help?

My problem is the same and i also can’t fix it :(

You can follow the steps in this article for uploading a file in Yii2 using the kartik\widgets\FileInput widget.

Secondly, I see that you are using multiple file uploads (multiple property is set to true in the widget). You must use an array naming format for multiple file uploads as mentioned in this article.

added this below code

view page

<?= $form->field($model, ‘photo’)->widget(FileInput::classname(), [

      'options' =&gt; ['accept' =&gt; 'photo/*'],


  ]); 


?&gt;

controller page

Yii::$app->params[‘uploadPath’] = Yii::$app->basePath . ‘/uploads/’;

  if (&#036;model-&gt;load(Yii::&#036;app-&gt;request-&gt;post())) {


      &#036;image = UploadedFile::getInstance(&#036;model, 'photo');


      &#036;ext = end((explode(&quot;.&quot;, &#036;image-&gt;name)));


      &#036;model-&gt;photo = time().&#036;model-&gt;id.&quot;.{&#036;ext}&quot;;   


      &#036;path = Yii::&#036;app-&gt;params['uploadPath'] . &#036;model-&gt;photo;


     


      if(&#036;model-&gt;save()){


        &#036;image-&gt;saveAs(&#036;path);


       


        return &#036;this-&gt;redirect(['view', 'id' =&gt; &#036;model-&gt;id]);


      }


    } else {


       


        return &#036;this-&gt;render('create', [


            'model' =&gt; &#036;model,


          


        ]);


    }

model page

[[‘photo’], ‘safe’],

[[‘photo’], ‘file’, ‘extensions’=>‘jpg, gif, png’],

Refer this follow-up wiki for details on how you can use the FileInput for preview, update, and delete.

Hi,

I face similar issue too.

UploadFile::getInstance($model,‘image’) is always return NULL.

In model have : public $image, [[‘image’], ‘safe’],[[‘image’], ‘file’, ‘extensions’=>‘jpg, png’],

In ActiveForm have : ‘options’ => [‘enctype’ => ‘multipart/form-data’]].

In image ActiveField have : ‘options’ => [‘accept’ => ‘image/*’].

I can not know where I am wrong while I did all tutorials from :

Anybody have any other ideas about this? Thanks.

Hi guys, I have also the same issue, I am getting always NULL, I have tried all step-by-step tutorials but no result.

If someone fixed this problem, please guys share the solution. Thanks!

Same Issue here,

Exactly did all the steps and made sure I’m not missing anything.

but the form does not upload file and the value of fileInput field is empty.

I did a test with YII’s internal fileInput() field and everything works well.

under bootstrap and adminLTE theme.

I had similar issue, fileinput field named as "imageFile" was empty when checking like


if($model->imageFile) ..

so before that i did this:


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

and now field is not empty