Answer the question
In order to leave comments, you need to log in
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]);
}
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
No such file or directory
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question