Returns human readable sizes. Based on original functions written by Aidan Lister and Quentin Zervaas.
echo Text::bytes(filesize($file));
string Core_Text::bytes( integer $bytes [, string $force_unit = null , string $format = null , boolean $si = bool true ] )
参数列表
参数 类型 描述 默认值 $bytes
integer
Size in bytes $force_unit
string
A definitive unit null $format
string
The return string format null $si
boolean
Whether to use SI prefixes or IEC bool true
string
public static function bytes($bytes, $force_unit = NULL, $format = NULL, $si = TRUE)
{
// Format string
$format = (null===$format) ? '%01.2f %s' : (string)$format;
// IEC prefixes (binary)
if ( false==$si || false!==strpos($force_unit, 'i'))
{
$units = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB');
$mod = 1024;
}
// SI prefixes (decimal)
else
{
$units = array('B', 'kB', 'MB', 'GB', 'TB', 'PB');
$mod = 1000;
}
// Determine unit to use
if ( ($power = array_search((string)$force_unit, $units)) === FALSE )
{
$power = ($bytes > 0) ? floor(log($bytes, $mod)) : 0;
}
return sprintf($format, $bytes / pow($mod, $power), $units[$power]);
}