note 111106 added to function.filesize
| From: | robert at NOSPAM dot woodst dot com | Date: | Tue, 15 Jan 2013 15:27:54 +0000 |
| Subject: | note 111106 added to function.filesize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-193075@lists.php.net to get a copy of this message | ||
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' : '');
}
?>
----
Server IP: 69.147.83.199
Probable Submitter: 69.255.131.117
----
Manual Page -- http://php.net/manual/en/function.filesize.php
Edit -- https://master.php.net/note/edit/111106
Del: integrated -- https://master.php.net/note/delete/111106/integrated
Del: useless -- https://master.php.net/note/delete/111106/useless
Del: bad code -- https://master.php.net/note/delete/111106/bad+code
Del: spam -- https://master.php.net/note/delete/111106/spam
Del: non-english -- https://master.php.net/note/delete/111106/non-english
Del: in docs -- https://master.php.net/note/delete/111106/in+docs
Del: other reasons-- https://master.php.net/note/delete/111106
Reject -- https://master.php.net/note/reject/111106
Search -- https://master.php.net/manage/user-notes.php