| 3 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* Smarty plugin
|
|
|
4 |
* -------------------------------------------------------------
|
|
|
5 |
* Type: modifier
|
|
|
6 |
* Name: fsize_format
|
|
|
7 |
* Version: 0.2
|
|
|
8 |
* Date: 2003-05-15
|
|
|
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
|
|
|
13 |
or
|
|
|
14 |
{$filesize|fsize_format:"MB"} => 123.45 MB
|
|
|
15 |
or
|
|
|
16 |
{$filesize|fsize_format:"TB":4} => 0.0012 TB
|
|
|
17 |
* Params:
|
|
|
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
|
|
|
24 |
* Version:
|
|
|
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 |
* -------------------------------------------------------------
|
|
|
30 |
*/
|
|
|
31 |
function smarty_modifier_fsize_format( $size, $format = '', $precision =
|
|
|
32 |
2, $dec_point = ".", $thousands_sep = "," )
|
|
|
33 |
{
|
|
|
34 |
$format = strtoupper( $format );
|
|
|
35 |
|
|
|
36 |
static $sizes = array();
|
|
|
37 |
|
|
|
38 |
if ( !count( $sizes ) )
|
|
|
39 |
{
|
|
|
40 |
$b = 1024;
|
|
|
41 |
$sizes["B"] = 1;
|
|
|
42 |
$sizes["KB"] = $sizes["B"] * $b;
|
|
|
43 |
$sizes["MB"] = $sizes["KB"] * $b;
|
|
|
44 |
$sizes["GB"] = $sizes["MB"] * $b;
|
|
|
45 |
$sizes["TB"] = $sizes["GB"] * $b;
|
|
|
46 |
|
|
|
47 |
$sizes = array_reverse( $sizes, true );
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
//~ get "human" filesize
|
|
|
51 |
foreach ( $sizes as $unit => $bytes )
|
|
|
52 |
{
|
|
|
53 |
if ( $size > $bytes || $unit == $format )
|
|
|
54 |
{
|
|
|
55 |
//~ return formatted size
|
|
|
56 |
return number_format( $size / $bytes, $precision, $dec_point,
|
|
|
57 |
$thousands_sep ) . " " . $unit;
|
|
|
58 |
} //~ end if
|
|
|
59 |
} //~ end foreach
|
|
|
60 |
} //~ end function
|
|
|
61 |
|
|
|
62 |
?>
|