note 105204 deleted from function.filesize by danbrown
| From: | danbrown@php.net | Date: | Tue, 02 Aug 2011 22:30:04 +0000 |
| Subject: | note 105204 deleted from function.filesize by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-182122@lists.php.net to get a copy of this message | ||
Note Submitter: wang.phillip
----
this is modified from arne's code below, which doesn't calculate the filesize correctly...
This should be the correct one:
function fsize($file) {
// filesize will only return the lower 32 bits of
// the file's size! Make it unsigned.
$fmod = filesize($file);
var_dump($fmod);
if ($fmod < 0) $fmod += 2.0 * (PHP_INT_MAX + 1);
var_dump($fmod);
// 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);
fseek($myfile, 1, SEEK_CUR);
fseek($myfile, PHP_INT_MAX, SEEK_CUR);
$i++;
}
fclose($myfile);
--$i;
// add the lower 32 bit to our 2 * PHP_INT_MAX multiplier
return ($i * 2.0 * (PHP_INT_MAX + 1)) + $fmod;
}