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];
}
← All posts