3

I want to upload an image and save it into my database. Here, the field name in database is image_path. When, I am trying to upload my image it shows an error: Call to a member function saveAs() on a non-object on line

$customer->file->saveAs('uploads/customer/' . $customer->file->baseName . '.' . $customer->file->extension);

If I print var_dump($customer->file); it returns NULL.

Can anyone help me to resolve this issue.

This is my view:

    <?php $form = ActiveForm::begin([
            'id' => 'my-profile',
            'action' => \Yii::$app->urlManager->createUrl(['/myprofile', 'id_customer' => Yii::$app->user->identity->id_customer]),                     
            'options' => ['enctype'=>'multipart/form-data']
    ]); ?>
    <?= $form->field($customer, 'file')->fileInput() ?>
    <?php ActiveForm::end(); ?>

This is my Model:

public $file;
public function rules()
{
    return [
        ['active', 'default', 'value' => self::STATUS_ACTIVE],
        ['active', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],            
        [['file'],'file'],
        [['name', 'image_path'], 'string', 'max' => 200],            
    ];
}

public function attributeLabels()
{
    return [            
        'file' => 'Profile Picture ',            
    ];
}

This is my controller:

public function actionMyprofile(){   

  $customer->file = UploadedFile::getInstance($customer,'image_path');      
  $customer->file->saveAs('uploads/customer/' . $customer->file->baseName . '.' . $customer->file->extension);
  $customer->file = 'uploads/customer/' . $customer->file->baseName . '.' . $customer->file->extension;
}
7
  • Replace image_path with file in controller, see your model rules. Commented Dec 14, 2015 at 12:12
  • UploadedFile::getInstance($customer,'image_path'); should be UploadedFile::getInstance($customer,'file');, since <?= $form->field($customer, 'file')->fileInput() ?> Commented Dec 14, 2015 at 13:17
  • 1
    I ask a some question some days ago but did not get the acurate answer Commented Dec 14, 2015 at 13:17
  • stackoverflow.com/questions/33119257/… Commented Dec 14, 2015 at 13:18
  • After doing this it remains same. Commented Dec 14, 2015 at 13:43

1 Answer 1

3

View:

<?php $form = ActiveForm::begin([
        'id' => 'my-profile',
        'action' => \Yii::$app->urlManager->createUrl(['/myprofile', 'id_customer' => Yii::$app->user->identity->id_customer]),                     
        'options' => ['enctype'=>'multipart/form-data']
]); ?>
<?= $form->field($customer, 'image_path')->fileInput() ?>
<?php ActiveForm::end(); ?>

Model:

public function rules()
{
  return [
    ['active', 'default', 'value' => self::STATUS_ACTIVE],
    ['active', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],            
    [['image_path'],'file'],
    [['name', 'image_path'], 'string', 'max' => 200],            
  ];
}

public function attributeLabels()
{
   return [            
       'image_path' => 'Profile Picture ',            
   ];
}

Controller:

public function actionCreate()
{
    $model = new YourModel_name();          

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

        $filename = pathinfo($model->image_path , PATHINFO_FILENAME);
        $ext = pathinfo($model->image_path , PATHINFO_EXTENSION);

        $newFname = $filename.'.'.$ext;

        $path=Yii::getAlias('@webroot').'/image/event-picture/';
        if(!empty($newFname)){
            $model->image_path->saveAs($path.$newFname);
            $model->image_path = $newFname; 
            if($model->save()){
                return $this->redirect(['your redirect_path', 'id' => $model->id]);
            }       
        }           
    } 
    return $this->render('create', [
        'model' => $model,
    ]);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.