public static function iec_file_size($size, $digits=4, $unit=0) {
$units = array('B','KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB');
while($size >= 1024) {
$size /= 1024;
++$unit;
}
return number_format($size, max(0,$digits-strlen((string)intval($size))), null, '').' '.$units[$unit];
}
$units = array('B','KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB');
while($size >= 1024) {
$size /= 1024;
++$unit;
}
return number_format($size, max(0,$digits-strlen((string)intval($size))), null, '').' '.$units[$unit];
}
3 thoughts on “Human Readable File Size in PHP”
Very nice !
Worked like a charm
Samuca
Less readable please
Wanderer
You want a less readable file size? Just output it in bytes then 😉
Mark