How to insert the data file into the database?


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


		$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,


but I have an error.

Ok, I got it.

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


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";


Here you can find good tutorial about 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.

YIi is fun!