summaryrefslogtreecommitdiffstats
path: root/lib/helper/FilesHelper.php
blob: ff290d67c4c883fed6c638684f5b4a37aae12a4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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;
}

?>