Page 1 of 1

parse csv file

Posted: Fri Aug 15, 2008 7:02 am
by jfarissi
hi
I parse a csv file under memory_limit = 128 without any problem but when I parse the file under memory_limit = 32 I receive the following message:
PHP Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 1943 bytes
while i want to execute the script under 32MB.

i can't change memory limit in my server

file size = 13567 ko
file lignes = 3509 lignes


can you help me

thanks

Re: parse csv file

Posted: Fri Aug 15, 2008 7:05 am
by onion2k
How are you opening the CSV file?

Re: parse csv file

Posted: Fri Aug 15, 2008 7:25 am
by jfarissi
my code is

set_time_limit(0);
$fichier="test.csv";
$tabfich=file($fichier);
echo count($tabfich);

for( $i = 0 ; $i < count($tabfich); $i++ )
{
$arraycsv[] = explode("|",$tabfich[$i],-1);
//$tabcsv[] = eval($arraycsv.$i)[$i];
}
print_r($arraycsv) ."</br>";

thanks

Re: parse csv file

Posted: Fri Aug 15, 2008 8:18 am
by onion2k
The file() function reads the entire file into memory. Then when you're putting it into $arraycsv[] you're creating a copy of the entire thing in a different area of memory. And then when you echo it as a string you'll create another copy in the output buffer. So by the end of the script you have three complete copies of the CSV in memory.

Try using fopen() and fgetcsv() or fgets() instead, that way it'll only be read in line by line, and output it as you go using flush (or ob_flush() depending on your server settings) to clear the output buffer and send the content to the user's browser. That way you'll be able to read any size of file with quite minimal amounts of memory.