Main Content

PHP fread - truncated data

Archive - Originally posted on "The Horse's Mouth" - 2007-02-27 15:21:48 - Graham Ellis

Note that PHP's fread function returns the number of bytes you request as the second parameter ...OR the number of remaining bytes if it's less than the number you give ... or 8192 bytes, even if there's more than that available and you've requested more.

I got caught out by this one earlier today ... felt that it was useful (re)minder to post it up here.

If you need to read more that 8k into a variable:

while ($info = fread($fh,8192)) {
$fullstuff .= $info;
}