Modified byte_convert() function to display file sizes correctly (based on 1024 convention), and include control over decimal places displayed.

This commit is contained in:
Nate Jones 2012-10-17 15:20:58 +00:00
parent c2f73ce243
commit 8e23f390c4
1 changed files with 26 additions and 31 deletions

View File

@ -357,16 +357,11 @@ function event_socket_request_cmd($cmd) {
fclose($fp); fclose($fp);
} }
function byte_convert( $bytes ) { function byte_convert($bytes, $decimals = 2) {
if ($bytes<=0) { if ($bytes <= 0) { return '0 Bytes'; }
return '0 Byte'; $convention = 1024;
} $formattedbytes = array_reduce( array(' B', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', 'ZB'), create_function( '$a,$b', 'return is_numeric($a)?($a>='.$convention.'?$a/'.$convention.':number_format($a,'.$decimals.').$b):$a;' ), $bytes );
return $formattedbytes;
$convention=1000; //[1000->10^x|1024->2^x]
$s=array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB');
$e=floor(log($bytes,$convention));
$e=floor(log($bytes,$convention));
return round($bytes/pow($convention,$e),2).' '.$s[$e];
} }
function lan_sip_profile() { function lan_sip_profile() {