How to insert the data file into the database?

Hi,

I’m using dropzonejs to upload files on the server but i would like to insert data files to my database. How to do it?


public function actionUpload()

{

    $fileName = 'file';

    $uploadPath = 'uploads';


    if (isset($_FILES[$fileName])) {

        $file = \yii\web\UploadedFile::getInstanceByName($fileName);


        //Print file data

        //print_r($file);

		$hash = substr(md5(time()), 0, 4);

        if ($file->saveAs($uploadPath . '/' . $hash . $file->name)) {

            

            echo \yii\helpers\Json::encode($file);

        }

    } else return $this->render('upload');


    return false;

}

I tried use this:


$connection->createCommand()->insert('user', [

    'name' => 'Sam',

    'age' => 30,

])->execute();

but I have an error.

Ok, I got it.


$query="INSERT INTO `galeria_drobne`(`id`, `idogl`, `nazwa`) VALUES ('','asd','sdsd')";

			Yii::$app->db->createCommand($query)->execute();

This is wrong approach. First make your galeria_drobne model using Gii tool, and then you can do it using the following code.




$model = new GaleriaDrobne();

$model->idogl = 'Your value';

$model->nazwa = "Your value";

$model->save(false);



Here you can find good tutorial about Gii Tool: http://code-epicenter.com/yii-framework-2-tutorial-5-working-with-gii-tool/

I’m still learning and I was thinking that I’m doing it wrong. Thanks.

Make sure you watch other great video tutorials about Yii 2 Framework. Post your problems here on forums, and try to follow some blogs that are writing about Yii Framework, or read some books.

http://www.yiiframework.com/tutorials/

http://www.yiiframework.com/screencasts/

http://code-epicenter.com

https://larry.pub/

YIi is fun!