note 113457 added to function.filesize
| From: | linda dot collins at mailinator dot com | Date: | Sun, 13 Oct 2013 02:08:10 +0000 |
| Subject: | note 113457 added to function.filesize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-196539@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 64.71.164.2
Probable Submitter: 65.129.170.99
----
Manual Page -- http://php.net/manual/en/function.filesize.php
Edit -- https://master.php.net/note/edit/113457
Del: integrated -- https://master.php.net/note/delete/113457/integrated
Del: useless -- https://master.php.net/note/delete/113457/useless
Del: bad code -- https://master.php.net/note/delete/113457/bad+code
Del: spam -- https://master.php.net/note/delete/113457/spam
Del: non-english -- https://master.php.net/note/delete/113457/non-english
Del: in docs -- https://master.php.net/note/delete/113457/in+docs
Del: other reasons-- https://master.php.net/note/delete/113457
Reject -- https://master.php.net/note/reject/113457
Search -- https://master.php.net/manage/user-notes.php