0

How can I read a big file in php (csv file) line by line to avoid getting out of memory? At the moment, I have this :

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 72 bytes)

I'm nearly sure my php reads lines one by one :

$i = 0;
    $source = fopen('falkcsvutf.csv', 'r') or die("Problem open file");
    while (($data = fgetcsv($source, 0, ";")) !== FALSE && $i < 10)
    {
        echo $data[1];
        $i++;
    }
    fclose($source);

$i < 10 is there only to limit in debugging...

I have the feeling php things there is only one line in my csv, however, when I open the file in sublime text, I see all the lines (with line numbering)

The file is a Excel file, saved to CSV, then converted to utf8 with iconv function.

any idea?

Vincent Duprez
  • 3,772
  • 8
  • 36
  • 76

1 Answers1

1

From the PHP docs:

Note: If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem.

So check the value of auto_detect_line_endings

Mark Baker
  • 209,507
  • 32
  • 346
  • 385