Skip to content

Instantly share code, notes, and snippets.

@advitum
Created July 17, 2014 12:59
Show Gist options
  • Save advitum/a431e130b41d6f96933f to your computer and use it in GitHub Desktop.
Save advitum/a431e130b41d6f96933f to your computer and use it in GitHub Desktop.
Makes file size human-readable
<?php
function humanFileSize($size, $german = true) {
$fileSizePrefixes = array('k', 'M', 'G', 'T', 'P');
$size = intval($size);
$prefix = -1;
while($size > 1024 / 5 && $prefix < count($fileSizePrefixes) - 1) {
$size /= 1024;
$prefix++;
}
$rounded = round($size * 10) / 10;
if($german) {
$rounded = str_replace('.', ',', $rounded);
}
return $rounded . ' ' . ($prefix >= 0 ? $fileSizePrefixes[$prefix] : '') . 'B';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment