I
I
ITwoolf2019-03-20 11:11:15
Yii
ITwoolf, 2019-03-20 11:11:15

How to fix file upload error?

Good afternoon! Finally I was able to successfully set up the import button in my plate. Decided to try it out. Made the export of the plate in csv format. Then I decided to import it myself. I click on the import button, then I select the same file and click "Submit"
As a result, the error fopen(D:\Server\OSPanel\domains\site2\frontend\runtime/1/grid-export.csv): failed to open stream pops up : No such file or directory
I went through my code and did not understand what the jamb is.
Controller:

public function actionImporter($fName)
    {
        //путь к файлу
        $pathToFile = Yii::getAlias('@way7') . '/'.$fName;
        //файл можно загрузить с помощью формы

        if (!file_exists($pathToFile) || !is_readable($pathToFile)) {
            //... код, если файл отсутствует
        }
        $data = array();

        if (($handle = fopen($pathToFile, 'r')) !== false) {
            $i = 0;

            while (($row = fgetcsv($handle, 1000, ',')) !== false) {
                $model = new Kniga1();
                $model->name = $row [0];
                $model->creation_date = $row [1];
                $model->id_avtor1 = $row [2];

                if ($model->validate()) {
                    $model->save();
                } else {
                    //... код в случае ошибки сохранения
                }
            }
            fclose($handle);
        };
        //... код после импорта
    }
    public function actionUpload()
    {
        Yii::setAlias('@way7', '@runtime/1');
        $model = new UploadForm();

        if (Yii::$app->request->isPost) {
            $model->way7 = UploadedFile::getInstance($model, 'way7');
            if ($model->upload()) {
                // file is uploaded successfully
                $this->actionImporter($model->fName);
                VarDumper::dump($model);
                return;
            }
        }
        return $this->render('upload', ['model' => $model]);
    }

Model:
public $fName;
    /**
     * @var UploadedFile
     */
    public $way7;

    public function rules()
    {
        return [
            ) ?>

    <?= $form->field($model, 'way7')->fileInput() ?>

    <button>Submit</button>

<?php ActiveForm::end() ?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2019-03-20
@ITwoolf

No such file or directory

what is not clear here? There is no such file or directory. The solution is to create such a file or directory. Programmatically or manually. I don't understand what the problem is. You have an unambiguous error and the path to the file that gives this error. What else do you need?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question