Format Bytes (IEC)

Format bytes using IEC units (KiB, MiB, GiB) based on powers of 1024.

Code

Utilities
$units = ['B', 'KiB', 'MiB', 'GiB', 'TiB'];
$size = $bytes;
$i = 0;
while ($size >= 1024 && $i < count($units) - 1) {
    $size /= 1024;
    $i++;
}
sprintf('%.2f %s', $size, $units[$i]);

Parameters

Size in bytes.

Server

More PHP Snippets