note 112806 added to function.filesize
| From: | Anonymous | Date: | Wed, 24 Jul 2013 22:57:28 +0000 |
| Subject: | note 112806 added to function.filesize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-195761@lists.php.net to get a copy of this message | ||
This functions returns the exact file size for file larger than 2 GB on 32 bit OS:
<?php
function file_get_size($file) {
//open file
$fh = fopen($file, "r");
//declare some variables
$size = "0";
$char = "";
//set file pointer to 0; I'm a little bit paranoid, you can remove this
fseek($fh, 0, SEEK_SET);
//set multiplicator to zero
$count = 0;
while (true) {
//jump 1 MB forward in file
fseek($fh, 1048576, SEEK_CUR);
//check if we actually left the file
if (($char = fgetc($fh)) !== false) {
//if not, go on
$count ++;
} else {
//else jump back where we were before leaving and exit loop
fseek($fh, -1048576, SEEK_CUR);
break;
}
}
//we could make $count jumps, so the file is at least $count * 1.000001 MB large
//1048577 because we jump 1 MB and fgetc goes 1 B forward too
$size = bcmul("1048577", $count);
//now count the last few bytes; they're always less than 1048576 so it's quite fast
$fine = 0;
while(false !== ($char = fgetc($fh))) {
$fine ++;
}
//and add them
$size = bcadd($size, $fine);
fclose($fh);
return $size;
}
?>
----
Server IP: 69.147.83.199
Probable Submitter: 84.189.254.202
----
Manual Page -- http://php.net/manual/en/function.filesize.php
Edit -- https://master.php.net/note/edit/112806
Del: integrated -- https://master.php.net/note/delete/112806/integrated
Del: useless -- https://master.php.net/note/delete/112806/useless
Del: bad code -- https://master.php.net/note/delete/112806/bad+code
Del: spam -- https://master.php.net/note/delete/112806/spam
Del: non-english -- https://master.php.net/note/delete/112806/non-english
Del: in docs -- https://master.php.net/note/delete/112806/in+docs
Del: other reasons-- https://master.php.net/note/delete/112806
Reject -- https://master.php.net/note/reject/112806
Search -- https://master.php.net/manage/user-notes.php