note 83303 deleted from function.filesize by aidan
| From: | aidan@php.net | Date: | Sat, 02 May 2009 10:33:14 +0000 |
| Subject: | note 83303 deleted from function.filesize by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-154106@lists.php.net to get a copy of this message | ||
Note Submitter: chris [AT] cmbuckley [DOT] co [DOT] uk
----
Yet another function for size readability:
<?php
function filesize_format($byte = 0, $format = null) {
$postfixes = 'KMGTPEZY'; $pow = 0;
do {
$pow++;
if (1024 > ($byte /= 1024)) {
$str = ($pow < strlen($postfixes) ? $postfixes[$pow] . 'iB' : '×
2<sup>' . ($pow * 10) . '</sup> B');
}
} while ($byte >= 1024);
$arr = array($byte, $str);
return (is_null($format) ? $arr : vsprintf("$format %s", $arr));
}
// array(2) { [0]=> float(10.8623046875) [1]=> string(3) "MiB" }
var_dump(filesize_format(11123));
// string(8) "10.86 MiB"
var_dump(filesize_format(11123, '%01.2f'));
// string(27) "82 × 2<sup>80</sup> B"
var_dump(filesize_format(99999999999999999999999999, '%d'));
?>