Bytes to Human Readable

Convert bytes to human readable format (KB, MB, GB, etc.) for file sizes.

Code

Utilities
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
$i = 0;
$size = $bytes;
while ($size >= 1024 && $i < count($units) - 1) { $size /= 1024; $i++; }
return sprintf("%.2f %s", $size, $units[$i]);

Parameters

Number of bytes.

Server

More PHP Snippets