Skip to content

Instantly share code, notes, and snippets.

@surdaft
Forked from richjenks/friendly-filesize.php
Last active August 25, 2017 09:25
Show Gist options
  • Save surdaft/3aa13b136dcbbc754e58dd3d5b90a184 to your computer and use it in GitHub Desktop.
Save surdaft/3aa13b136dcbbc754e58dd3d5b90a184 to your computer and use it in GitHub Desktop.
<?php
function humanize_filesize($size, $precision = 0) {
if ($size !== 0) {
$base = log($size) / log(1024);
$units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$size = round(pow(1024, $base - floor($base)), $precision);
$unit = $units[floor($base)];
} else {
$unit = 'B';
}
return "{$size} {$unit}";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment