View helper for printing file-size
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 | /** * ViewHelper for printing human-readable file-sizes * @author Jakub Truneček <jakub@trunecek.net> */ class Zend_View_Helper_FileSize extends Zend_View_Helper_Abstract{ /** * Acceptable prefices of SI * @var array */ protected static $_prefixes = array('', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'); /** * Tranformation to huma-readable format * @param int $size Size in bytes * @param int $precision Presicion of result (default 2) * @return string Transformed size */ public function fileSize($size, $precision = 2) { $result = $size; $index = 0; while ($result > 1024 && $index < count(self::$_prefixes)) { $result = $result / 1024; $index++; } return sprintf('%1.' . $precision . 'f %sB', $result, self::$_prefixes[$index]); } } |
Comments
You must login before commenting on a snippet. If you do not have an account, please register.