4 * -------------------------------------------------------------
9 * Author: Joscha Feth, joscha@feth.com
10 * Purpose: formats a filesize (in bytes) to human-readable format
11 * Usage: In the template, use
12 {$filesize|fsize_format} => 123.45 B|KB|MB|GB|TB
14 {$filesize|fsize_format:"MB"} => 123.45 MB
16 {$filesize|fsize_format:"TB":4} => 0.0012 TB
18 int size the filesize in bytes
19 string format the format, the output shall be: B, KB, MB, GB or TB
20 int precision the rounding precision
21 string dec_point the decimal separator
22 string thousands_sep the thousands separator
23 * Install: Drop into the plugin directory
25 * 2003-05-15 Version 0.2 - added dec_point and thousands_sep thanks to Thomas Brandl, tbrandl@barff.de
26 * - made format always uppercase
27 * - count sizes "on-the-fly"
28 * 2003-02-21 Version 0.1 - initial release
29 * -------------------------------------------------------------
31 function smarty_modifier_fsize_format($size,$format = '',$precision = 2, $dec_point = ".", $thousands_sep = ",")
34 $format = strtoupper($format);
36 static $sizes = array();
41 $sizes["KB"] = $sizes["B"] * $b;
42 $sizes["MB"] = $sizes["KB"] * $b;
43 $sizes["GB"] = $sizes["MB"] * $b;
44 $sizes["TB"] = $sizes["GB"] * $b;
46 $sizes = array_reverse($sizes,true);
49 //~ get "human" filesize
50 foreach($sizes AS $unit => $bytes) {
51 if($size > $bytes || $unit == $format) {
52 //~ return formatted size
53 return number_format($size / $bytes,$precision,$dec_point,$thousands_sep)." ".$unit;