My company gets a set of CSV files full of bank account info each month that I need to import into a database. Some of these files can be pretty big. For example, one is abo
You can use generator for memory efficient file ready. The small snippet below might help you.
#Method
public function getFileRecords($params)
{
$fp = fopen('../' . $params['file'] . '.csv', 'r');
//$header = fgetcsv($fp, 1000, ','); // skip header
while (($line = fgetcsv($fp, 1000, ',')) != FALSE) {
$line = array_map(function($str) {
return str_replace('\N', '', $str);
}, $line);
yield $line;
}
fclose($fp);
return;
}
#Implementation
foreach ($yourModel->getFileRecords($params) as $row) {
// you get row as an assoc array;
$yourModel->save($row);
}