Hello,

"Jeff Schwartz" <[EMAIL PROTECTED]> 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]...


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to