parse csv file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jfarissi
Forum Newbie
Posts: 4
Joined: Fri Aug 15, 2008 3:25 am

parse csv file

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: parse csv file

Post by onion2k »

How are you opening the CSV file?
jfarissi
Forum Newbie
Posts: 4
Joined: Fri Aug 15, 2008 3:25 am

Re: parse csv file

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: parse csv file

Post 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.
Post Reply