http://php.net/manual/en/language.generators.overview.php
Calling range(0, 1000000) will result in well over 100 MB of memory being used.
As an alternative, we can implement an xrange() generator, which will only ever need enough memory to create an Iterator object and track the current state of the generator internally, which turns out to be less than 1 kilobyte.
http://mark-story.com/posts/view/php-generators-a-useful-example
http://www.sebastianviereck.de/en/ver-abreiten-large-files-with-php-using-generator/
Hope this examples will help you, short answer is: use generators!
// This example I think is fine!
<?php
class CsvFile {
protected $file;
public function __construct($file) {
$this->file = fopen($file, 'r');
}
public function parse() {
while (!feof($this->file)) {
yield fgetcsv($this->file);
}
return;
}
}
$csv = new CsvFile('/pathTo/file.csv');
foreach ($csv->parse() as $row) {
echo $row;
// INSERT INTO DATABASE
}