diff options
Diffstat (limited to 'lib/helper/FilesHelper.php')
-rw-r--r-- | lib/helper/FilesHelper.php | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/helper/FilesHelper.php b/lib/helper/FilesHelper.php new file mode 100644 index 0000000..ff290d6 --- /dev/null +++ b/lib/helper/FilesHelper.php @@ -0,0 +1,27 @@ +<?php + +function format_bytes($val, $digits = 3, $mode = "SI", $bB = "B") +{ + $si = array("", "k", "M", "G", "T", "P", "E", "Z", "Y"); + $iec = array("", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"); + + switch(strtoupper($mode)) + { + case "SI" : $factor = 1000; $symbols = $si; break; + case "IEC" : $factor = 1024; $symbols = $iec; break; + default : $factor = 1000; $symbols = $si; break; + } + switch($bB) + { + case "b" : $val *= 8; break; + default : $bB = "B"; break; + } + for($i=0;$i<count($symbols)-1 && $val>=$factor;$i++) + $val /= $factor; + $p = strpos($val, "."); + if($p !== false && $p > $digits) $val = round($val); + elseif($p !== false) $val = round($val, $digits-$p); + return round($val, $digits) . " " . $symbols[$i] . $bB; +} + +?> |