note 17017 deleted from function.filesize by jimw
| From: | jimw@php.net | Date: | Fri, 14 Dec 2001 20:17:59 +0000 |
| Subject: | note 17017 deleted from function.filesize by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-22723@lists.php.net to get a copy of this message | ||
To solve the 1000/1024 multipliers problems, I suggest to use the IEC (International
Electrotechnical Commission) prefixes (see http://www.technick.net/guide_tables_prefix.php
at the end of page)
Note that b=bit and B=byte.
Take a look at the examples below:
// ------------------------------------------------------------
// return size (in bytes) rounded to 2 decimals and using IEC prefixes
// see: http://www.technick.net/guide_tables_prefix.php
// ------------------------------------------------------------
function formatIECByteSize($size) {
$exp = 0;
while($size >= pow(1024,($exp+1))) {$exp++;}
$ext=array("B","KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB");
$newsize = round($size/pow(1024,$exp),2);
$newsize .= $ext[$exp]; //attach IEC prefix
return ($newsize);
}
// ------------------------------------------------------------
// return size (in bytes) rounded to 2 decimals and using SI prefixes
// see: http://www.technick.net/guide_tables_prefix.php
// ------------------------------------------------------------
function formatSIByteSize($size) {
$exp = 0;
while($size >= pow(1000,($exp+1))) {$exp++;}
$ext=array("B","kB","MB","GB","TB","PB","EB","ZB","YB");
$newsize = round($size/pow(1000,$exp),2);
$newsize .= $ext[$exp]; //attach SI prefix
return ($newsize);
}
----------------------
Sincerely, Nicola Asuni
www.technick.net