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

I want to upload the image and i want to save the image name in database and move the image to the folder…

But now i can’t save any thing.

After uploading the file and submit the form the following error will be displayed


Error (#2)

An internal server error occurred.

My codes are 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 what error in this code

Kindly help me to fix this please.

Thanks

check below


$image = UploadedFile::getInstance($model, 'varImage');

$model->varImage= $image->name;

$path = Yii::$app->params['uploadPath'] . $model->varImage;

Thank you MR webin.

Now the image name saved in the database but the file was not moved to the particular folder

$image->saveAs(\Yii::$app->basePath.’/web/uploads/’.$image->name);

thanx

mark ‘+’

Now there is a same problem the uploaded file will not move to the selected folder

have u check that "uploads" folder exist in "web" folder?

Ofcourse the uploads folder is placed inside the web folder

My controller code:


$model = new Usertype();

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

{	

$no=rand(0,1000);

$image = UploadedFile::getInstance($model, 'varImage');

$model->varImage= $no.$image->name;

$path = Yii::$app->params['uploadPath'] . $model->varImage;  ///  $model->varImage

if($model->save())	

{		

if($image != '')

{

$image->saveAs(\Yii::$app->basePath.'/web/uploads/'.$image->name);

}

//$image->saveAs($path);

Yii::$app->getSession()->setFlash('error', 'User type inserted successfully');

return Yii::$app->getResponse()->redirect('/yii2/site/usertype');

}

}

echo \Yii::$app->basePath.’/web/uploads/’.$image->name;

nd check what u get.

It displays the path and filename. But the file will not moved to the uploads folder

kindly share ur code again.

My Controller code:


$model = new Usertype();

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

{	

$no=rand(0,1000);

$image = UploadedFile::getInstance($model, 'varImage');

$model->varImage= $no.$image->name;

$path = Yii::$app->params['uploadPath'] . $model->varImage;  ///  $model->varImage

if($model->save())	

{		

if($image != '')

{

$image->saveAs(\Yii::$app->basePath.'/web/uploads/'.$image->name);


}

//$image->saveAs($path);

Yii::$app->getSession()->setFlash('error', 'User type inserted successfully');

return Yii::$app->getResponse()->redirect('/yii2/site/usertype');

}

}



If anything wrong in the above posted code

$image->saveAs(\Yii::$app->basePath.’/web/uploads/’.$image->name)

before $model-save()


$image = UploadedFile::getInstance($model, 'varImage');

if($image != '')

{

$image->saveAs(\Yii::$app->basePath.'/web/uploads/'.$image->name);

}

 

if($model->save())      

{

  

}