sms
sms

Reputation: 103

can't upload image in yii2 using kartik widget

This is my controller i am get confused so let me know what is mistake new here

 $imgName = $model->uploadimg;
            $model->file = UploadedFile::getInstance($model, 'uploadimg');
            $model->file->saveAS('uploads/'.$imgName.'.'.$model->file->extension);
            $model->uplaodimg='uploads/'.$imgName.'.'.$model->file->extension;

 This is view 
echo $form->field($model, 'uploadimg')->widget(FileInput::classname(), [
    'options' => ['accept' => 'image/*'],
]);

Upvotes: 0

Views: 298

Answers (2)

Vishal Kumar
Vishal Kumar

Reputation: 21

View:

use yii\web\UploadedFile;

<?= $form->field($model, 'uploadimg')->fileInput(['multiple' => true, 'accept' => 'image/']) ?>

Controller:

public function actionCreate()
{
        $model = new Model_Name();
        if ($model->load(Yii::$app->request->post())) {
            $model->uploadimg = UploadedFile::getInstance($model, 'uploadimg');
            $model->save();                
        }
}

Upvotes: 0

Nitin Pund
Nitin Pund

Reputation: 1092

Set Activeform's option enctype to multipart/form-data as follows:

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

Upvotes: 1

Related Questions