note 111106 deleted from function.filesize by danbrown
| From: | danbrown@php.net | Date: | Thu, 17 Jan 2013 15:13:58 +0000 |
| Subject: | note 111106 deleted from function.filesize by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-193129@lists.php.net to get a copy of this message | ||
Note Submitter: robert at NOSPAM dot woodst dot com
----
Rommel posted a nice human_filesize() implementation, and I wanted to post a few refinements I made.
The biggest change is I limited the factor to the range of the $sz variable, rather than used error
suppression. I also used the IEC/NIST suggested abbreviations.
<?php
function human_filesize($filesize, $decimal_places=2) {
$sz = 'BKMGTPEZY';
$factor = min(max(0, floor(strlen($size) / 3)), 8);
return sprintf("%.{$decimal_places}f ", $size / pow(1024, $factor)) . $sz[$factor] .
($factor > 0 ? 'iB' : '');
}
?>