Discussion:
Allowed memory size exhausted
Jeff Schwartz
2002-12-07 00:56:12 UTC
Permalink
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;





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;

but get a similar error on the line "$contents = substr($contents,$size+1);" which is what I'm using to try to avoid running out of memory. It appears that I can't replace the variable with a smaller version of itself.

So, if I can't write out the whole thing and can't section it either, what can I do?

Jeff



---------------------------------
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now
@ Edwin
2002-12-07 05:54:01 UTC
Permalink
Hello,
Post by Jeff Schwartz
I have a large amount of data (1,948,280 bytes) that I tried to write out to a file using
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,
Post by Jeff Schwartz
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
Post by Jeff Schwartz
$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]...
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Continue reading on narkive:
Loading...