Gautham.M
Gautham.M

Reputation: 458

Yii2 Image name not saved in database but saved in folder

I want to upload an image. But the image name not saved in the database but the image moved to the specific folder.

But there no errors are displayed

In my controller code:

            if($model->load(Yii::$app->request->post()) && $model->save()) {
            $image = UploadedFile::getInstance($model,'varImage');

            $imagepath='uploads/';

            $no=rand(0,1000);

            if($image != '')
            {
            // $img_loc = "portfolio_{$rand_name}-{$image}"; 
            $img_loc='portfolio_'.$no.$image;
            $model->varImage = $img_loc;
            }
            if($image != '')
            {           
            $image->saveAs($imagepath.$model->varImage);
            } 
            Yii::$app->getSession()->setFlash('succ', 'Portfolio added successfully');          
            return $this->redirect('view_portfolio');
            }

In my model:

            public function rules()
            {
            return [

            ['varTitle', 'required'],
            [['varImage'], 'image', 'skipOnEmpty' => true],
            ['intOrder', 'required'],
            ['intOrder', 'unique'],
            ['intOrder', 'number'],
            ['intHomepage', 'safe'],


            ];
            }

In my View:

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

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

             ActiveForm::end();

             ?>

What's wrong in my code. Kindly help me to fix this. Thanks.

Upvotes: 0

Views: 1269

Answers (3)

SiZE
SiZE

Reputation: 2267

You save model before assigning image attribute.

Do it this way:

if ($model->load(Yii::$app->request->post()) && $model->validate()) {
   // your logic here
   $model->varImage = $img_loc;
   $image->saveAs($imagepath.$model->varImage);
   if ($model->save(false)) {
      // on success
   }
}

Upvotes: 1

jithin
jithin

Reputation: 920

change your upload code like below:

 if($model->load(Yii::$app->request->post())) {
                $image = UploadedFile::getInstance($model,'varImage');

                $imagepath='uploads/';

                $no=rand(0,1000);

                if($image != '')
                {
                $img_loc = "portfolio{$no}-{$image}"; 

                $model->varImage = $img_loc;
                }
                 if($model->save()){
                if($image)
                {           
                $image->saveAs($imagepath.$model->varImage);
                } 
            }
                Yii::$app->getSession()->setFlash('succ', 'Portfolio added successfully');          
                return $this->redirect('view_portfolio');
                }

use $model->save() after change image name.

Upvotes: 0

leninhasda
leninhasda

Reputation: 1730

UploadedFile::getInstance returns an UploadedFile object not a file name.

$image = UploadedFile::getInstance($model,'varImage');
// after this
// $image->baseName  <--- will have original file base name
// $image->extension <--- will have file extension
// $image->name      <--- will have original full file name

and the part you are doing wrong is here:

$img_loc='portfolio_'.$no.$image;

// do this
$img_loc='portfolio_'.$no.$image->name;

and also you are saving your model before setting above value

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

you need to call the $model->save() after you processed your image and set the image name.

Upvotes: 1

Related Questions