Answer the question
In order to leave comments, you need to log in
How to import csv files into table/database?
Good afternoon. I will try to describe my problem as succinctly as possible. I have a table of books with the fields ID, Name, Description, Author, Creation_Date and Date_Of_Change. I made the columns in the view using DynaGrid and therefore I already have the opportunity to export goods in csv format. Export files are stored in the download.
- I added another "Import" button in the view - I
registered the code for import in the controller, which I connected with the Import button.
Code in controller:
public function actionImporter()
{
//путь к файлу
$pathToFile = Yii::getAlias('@way7');
//файл можно загрузить с помощью формы
if (!file_exists($pathToFile) || !is_readable($pathToFile)) {
//... код, если файл отсутствует
}
$data = array();
if (($handle = fopen($pathToFile, 'r')) !== false) {
$i = 0;
while (($row = fgetcsv($handle, 1000, $delimiter)) !== 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);
};
//... код после импорта
}
Yii::setAlias('@way7', '/Загрузки/grid-export');
Answer the question
In order to leave comments, you need to log in
Writes that the directory is not found. Your folder path is not complete. You specify /loads....
Start searching from the root. I'm not sure that your downloads are in the root. Most likely they are in the web folder. Your path must be complete from the root to this folder. Or use Alial web. @web/downloads/....
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question