Re: Allowed memory size exhausted

From: Date: Sat, 07 Dec 2002 05:54:01 +0000
Subject: Re: Allowed memory size exhausted
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-127370@lists.php.net to get a copy of this message
Hello, "Jeff Schwartz" <jeff_schwartz@yahoo.com> wrote: > > I have a large amount of data (1,948,280 bytes) that I tried to write out to a file using > > if ($fp = fopen($file,"w")): > > fwrite($fp,$contents,strlen($contents)); > > fclose($fp); > > endif; I'm not sure if I understand this correctly but don't you think you don't need strlen($contents) up there? Also, try fopen($file,"wb"). And here, > and got "Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 1948281 bytes)" on the 2nd line. So, I tried to write out smaller portions with: > > if ($fp = fopen($file,"w")): > > $size = 4096; > > while (strlen($contents)){ > > $temp = substr($contents,0,$size); > > fwrite($fp,$temp,$size); > > $contents = substr($contents,$size+1); > > } > > fclose($fp); > > endif; ...doing "$contents = substr($contents,$size+1);" would actually cause you to lose some data. Try this instead: if ($fp = fopen($file,"wb")): // add "b" $size = 4096; while (strlen($contents)){ $temp = substr($contents,0,$size); fwrite($fp,$temp); // remove $size $contents = substr($contents,$size); // remove "+1" } fclose($fp); endif; I didn't test so I'm not sure if it'll work--just some ideas... HTH, - E ...[snip]...

« previous php.general (#127370) next »