Pyh.conf’25: a new PHP conference for the Russian-speaking community

Voting

: six minus three?
(Example: nine)

The Note You're Voting On

linda dot collins at mailinator dot com
11 years ago
A fast implementation that determines actual file size of large files (>2GB) on 32-bit PHP:

function RealFileSize($fp)
{
$pos = 0;
$size = 1073741824;
fseek($fp, 0, SEEK_SET);
while ($size > 1)
{
fseek($fp, $size, SEEK_CUR);

if (fgetc($fp) === false)
{
fseek($fp, -$size, SEEK_CUR);
$size = (int)($size / 2);
}
else
{
fseek($fp, -1, SEEK_CUR);
$pos += $size;
}
}

while (fgetc($fp) !== false) $pos++;

return $pos;
}

Input is an open file handle. Return value is an integer for file sizes < 4GB, floating-point otherwise.

This starts out by skipping ~1GB at a time, reads a character in, repeats. When it gets into the last GB, it halves the size whenever the read fails. The last couple of bytes are just read in.

Some people might have concerns over this function because $pos will become a floating point number after exceeding integer limits and they know of floating point's tendencies to be inaccurate. On most computers that correctly implement the IEEE floating point spec, $pos will be accurate out to around 9 *petabytes*. Unless you are working with multi-petabyte files in PHP or the code is executing on strange hardware, this function is going to be more than sufficient. Every part of this function has been carefully crafted to deal with 32-bit deficiencies.

<< Back to user notes page

To Top