save files in database


I need your help

I have a problem with saving multiple files into a database

Here is my code

public function actionUpload(){

   $model = new Galimg();

        if (Yii::$app->request->isPost) {



            $img = UploadedFile::getInstances($model, 'img');

            if ($model->validate()) { 


                foreach ($img as $file) {


                        $sfile = Yii::$app->security->generateRandomString() . '.' . $file->extension;

                        $model->img = $sfile;



                        $model->isNewRecord = true;

                   if( $model->save()){


                        $file->saveAs('uploads/galeria/' . $sfile);              





            return $this->redirect(['galeria/admin']);

        }else {

            return $this->render('upload', [

                'model' => $model,




files are copied correctly but the database is stored only the last file

I’m not sure what your model columns are but I guess the key field is in the post values and these are not changing - so the record is just overwritten each time. Do you have an auto column or should you call new model() with a unique key.