public function loadDataLocalInfile($table, $columns, $fileName)
    {
        $filePath = Storage::disk('local')->path($fileName);
        $fields = '';
        foreach ($columns as $key => $column) {
            $fields .= $column . ',';
        }
        $fields = rtrim($fields, ',');
        DB::raw("
            LOAD DATA LOCAL INFILE '" . $filePath . "' 
            INTO TABLE temporary_" . $table . "
            FIELDS TERMINATED BY ';'
            ENCLOSED BY '\"'
            LINES TERMINATED BY '\\n'
            IGNORE 1 LINES (" . $fields . ")");
        $updateColumns = '';
        foreach ($columns as $key => $column) {
            $updateColumns .= $column . ' = VALUES(' . $column . '),';
        }
        $updateColumns = rtrim($updateColumns, ',');
        DB::raw('
            INSERT INTO ' . $table . '
            SELECT * FROM temporary_' . $table . '
            ON DUPLICATE KEY UPDATE ' . $updateColumns);
    }

When the script is executed no errors and no data inserted to the DB, all tables exists, when i do

print_r(Storage::disk('local')->get($fileName));

CSV file is printed

"name";"image"
"userNameA";"urlImageA"
"userNameB";"urlImageB"
"userNameC";"urlImageC"

I added PDO::MYSQL_ATTR_LOCAL_INFILE => true in options to mysql in config/database.php

0 Answers