Move_uploaded_file(file): failed to open stream: No such file or directory

im trying to import some file using the yii2 to database but it didn’t work, did anyone know what is going on? Thank you

The place where you should start your search is: https://www.yiiframework.com/doc/guide/2.0/en/input-file-upload

File path incorrect or file not exist.

Did you take a look into tutorial that I gave you in my last post?

If you did, please provide additional code snippet.

Yess I looked down at the tutorial that you gave me, but i dont understand where is my fault

Thank you for your help

1 Like

ohh i got frustrated…

https://drive.google.com/open?id=1T3nxaQvzojXz0_7ZM6G_YrBIjwnS2SwS

Try this code instead of yours.

// Import model
<?php

namespace backend\models;

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

class ImportForm extends Model
{
    /**
     * @var UploadedFile
     */
    public $excelFile;

    public $filename;

    public $path;

    public function rules()
    {
        return [
            [['excelFile'], 'file', 'extensions' => 'xls, xlsx, xls, csv','maxSize' => 5120 * 1000,'skipOnEmpty' => false],
        ];
    }

    /**
     * @return bool
     * @throws \yii\base\Exception
     */
    public function upload()
    {
        if ($this->validate()) {
            // backend/web/files
            $this->path ='web/files';
            $this->filename = Yii::$app->session['kodeimport'] . '-' . Yii::$app->user->identity->username . '.' . $this->excelFile->extension;
            $this->excelFile->saveAs($this->path . DIRECTORY_SEPARATOR . $this->filename);
            return true;
        } else {
            return false;
        }
    }

   /**
     * 
     * @return string
     */
    public function getFullPath()
    {
        return $this->path. DIRECTORY_SEPARATOR. $this->filename;
    }
}
// Import controller
<?php 

namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\web\UploadedFile;
use backend\models\ImportForm;

class ImportController extends Controller
{
    public function actionImport()
    {
        $model = new ImportForm();
        if (Yii::$app->request->isPost) {
            $model->excelFile = UploadedFile::getInstance($model, 'excelFile');
            if ($model->upload()) {
                Yii::$app->session->setFlash('success', 'You have successfully uploaded file');
            } else {
              Yii::$app->session->setFlash('error',  'You have unsuccessfully uploaded file');
            }
        }

        return $this->render('yourview', ['model' => $model]);
    }
}
1 Like

@oim hank you for your help, it help me a lot… its working now

1 Like

I’m glad you’ve successfully solved the task.

1 Like