0

File uploading to uploads folder but not saving in the database, getting below error

Unknown Method – yii\base\UnknownMethodException Calling unknown method: app\models\UploadForm::save()

Below is my controller code

$name =  $model->file->baseName . '.' .$model->file->extension;

$file = new UploadForm();
$file->image_name = $name;
$file->image_path = $name;
$file->save();

This is my model code

namespace app\models;

use yii\base\Model;
use yii\web\UploadedFile;

  public function rules()
    {
        //echo "in";
        return [
           [['image'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg, jpeg'],
           ['image_path','string'],
        ];
    }

Can anyone help me? I am new to yii2

5
  • 1
    The error means that your UploadForm doesn't have save() method but you are trying to call it. It's probably because the UploadForm extends yii\base\Model that doesn't have the save() method. You should explain more about what you are trying to do and maybe show a little bit more of your code. Commented Nov 11, 2019 at 11:57
  • how to use this save method in namespace inside my code in yii2 Commented Nov 11, 2019 at 12:35
  • as @MichalHynčica said your UploadForm Model does extends \yii\db\ActiveRecord? Commented Nov 11, 2019 at 13:19
  • Tq u for your reply, actually i did not created model for table, i just created only model for form, that was the problem Commented Nov 12, 2019 at 6:03
  • $file->saveAs($destinationfilePath); and pass $destinationfilePath where you want to save it in your application Commented Nov 16, 2019 at 13:39

1 Answer 1

0

Can you try below code ?

use yii\web\UploadedFile;
$file = UploadedFile::getInstanceByName('Image Input Field Name');
if (!empty($file) && $file->extension != '') {
    $newFileName = rand() . '.' . $file->extension; 
    if ($file->saveAs($uploadPath . '/' . $newFileName)) {
    
        // another code
    }
}
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.