php - yii2 image not saved in database and can't move to folder -


i want upload image , want save image name in database , move image folder..

but can't save thing.

after uploading file , submit form following error displayed

error (#2) internal server error occurred. 

my codes below

in views:

<div class="row">     <div class="col-lg-5">     <?php $form = activeform::begin(['options' => ['enctype' =>   'multipart/form-data']]) ?>         <?php echo  $form->field($model, 'varimage')->fileinput() ?>         <div class="form-group">         <?php echo html::submitbutton('add', ['class' => 'btn btn-primary',  'name' => 'signup-button']) ?>         </div>     <?php activeform::end(); ?> </div> 

in models:

class usertype extends \yii\db\activerecord {     public function rules()     {         return [             [['varimage'], 'safe'],             [['varimage'], 'file', 'extensions'=>'jpg, gif, png'],         ];     }      public function attributelabels()     {         return [             'varimage' => 'image',         ];     } } 

in controller:

$model = new usertype(); if ($model->load(yii::$app->request->post()) )  {        $image = uploadedfile::getinstance($model, 'varimage');     $path = yii::$app->params['uploadpath'] . $model->varimage;     if($model->save())     {         $image->saveas($path);         yii::$app->getsession()->setflash('error', 'user type inserted successfully');         return yii::$app->getresponse()->redirect('/yii2/site/usertype');     } } 

i can't know error in code

kindly me fix this.

thanks

i think view , model code ok. try this. controller code. has worked me.

if ($model->load(yii::$app->request->post()) && $model->save()) {                $image = uploadedfile::getinstance($model, 'varimage');               if(!empty($image)){                   $image->saveas('uploads/' . $image->basename . '.' . $image->extension);                   $model->your_database_field_name = $image->basename . '.' . $image->extension;                   $model->save();               }                return $this->redirect(['view', 'id' => $model->id]);           } 

Comments