<?php
function size($size, array $options=null) {
$o = [
'binary' => false,
'decimalPlaces' => 2,
'decimalSeparator' => '.',
'thausandsSeparator' => '',
'maxThreshold' => false, 'sufix' => [
'thresholds' => ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'],
'decimal' => ' {threshold}B',
'binary' => ' {threshold}iB'
]
];
if ($options !== null)
$o = array_replace_recursive($o, $options);
$count = count($o['sufix']['thresholds']);
$pow = $o['binary'] ? 1024 : 1000;
for ($i = 0; $i < $count; $i++)
if (($size < pow($pow, $i + 1)) ||
($i === $o['maxThreshold']) ||
($i === ($count - 1))
)
return
number_format(
$size / pow($pow, $i),
$o['decimalPlaces'],
$o['decimalSeparator'],
$o['thausandsSeparator']
) .
str_replace(
'{threshold}',
$o['sufix']['thresholds'][$i],
$o['sufix'][$o['binary'] ? 'binary' : 'decimal']
);
}
var_dump(size(disk_free_space('/')));
var_dump(size(disk_free_space('/'), ['binary' => true]));
var_dump(size(disk_free_space('/'), ['maxThreshold' => 2]));
var_dump(size(disk_free_space('/'), ['binary' => true, 'maxThreshold' => 2]));
?>