Human Readable File Size in PHP

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];
}
Posted in

3 thoughts on “Human Readable File Size in PHP

  1. You want a less readable file size? Just output it in bytes then 😉

Leave a Reply

Your email address will not be published. Required fields are marked *