note 102135 added to function.filesize
| From: | arne_elster at web dot de | Date: | Fri, 28 Jan 2011 14:27:57 +0000 |
| Subject: | note 102135 added to function.filesize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-176430@lists.php.net to get a copy of this message | ||
Function that determines a file's size only with PHP functions,
for all files, also those > 4 GB:
<?php
function fsize($file) {
// filesize will only return the lower 32 bits of
// the file's size! Make it unsigned.
$fmod = filesize($file);
if ($fmod < 0) $fmod += 2.0 * (PHP_INT_MAX + 1);
// find the upper 32 bits
$i = 0;
$myfile = fopen($file, "r");
// feof has undefined behaviour for big files.
// after we hit the eof with fseek,
// fread may not be able to detect the eof,
// but it also can't read bytes, so use it as an
// indicator.
while (strlen(fread($myfile, 1)) === 1) {
fseek($myfile, PHP_INT_MAX, SEEK_CUR);
$i++;
}
fclose($myfile);
// $i is a multiplier for PHP_INT_MAX byte blocks.
// return to the last multiple of 4, as filesize has modulo of 4 GB (lower 32 bits)
if ($i % 2 == 1) $i--;
// add the lower 32 bit to our PHP_INT_MAX multiplier
return ((float)($i) * (PHP_INT_MAX + 1)) + $fmod;
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 195.37.186.61
----
Manual Page -- http://www.php.net/manual/en/function.filesize.php
Edit -- https://master.php.net/note/edit/102135
Del: integrated -- https://master.php.net/note/delete/102135/integrated
Del: useless -- https://master.php.net/note/delete/102135/useless
Del: bad code -- https://master.php.net/note/delete/102135/bad+code
Del: spam -- https://master.php.net/note/delete/102135/spam
Del: non-english -- https://master.php.net/note/delete/102135/non-english
Del: in docs -- https://master.php.net/note/delete/102135/in+docs
Del: other reasons-- https://master.php.net/note/delete/102135
Reject -- https://master.php.net/note/reject/102135
Search -- https://master.php.net/manage/user-notes.php