Program & Design Tips, tricks, tutorials, and tools on programming & web design

11Jul/123

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];
}
Filed under: PHP Leave a comment
Comments (3) Trackbacks (0)
  1. Very nice !

    Worked like a charm

  2. Less readable please

  3. You want a less readable file size? Just output it in bytes then ;)


Leave a comment

No trackbacks yet.