Reading a 6MB csv file in PHP - Memory exhausted -
i'm trying open/read 6mb csv file php so:
$lines = file("/path/to/my/file.csv");
but i'm getting following error:
php fatal error: allowed memory size of 1073741824 bytes exhausted (tried allocate 6461150 bytes) in /path/to/php_file.php on line 488
as can see, our php memory settings set 1gb (dedicated server, not limited shared hosting package etc).
my question should 6mb csv file really using over 1gb when read variable (array). i'm bit confused because i'm sure i've opened larger csv files php before without problems on server.
shouldn't make difference we're using php 5.3 on ubuntu 12.04 server.
you should process csv line line prevent memory exhaustion. try this:
$f = fopen("my.csv", "r"); if(true === is_resource($f)) { while(false === feof($f)) { $line = fgetcsv($f, 8192); // data here } fclose($f); }
using fgetcsv() parse csv far easier using file().
Comments
Post a Comment