Skip to content

Instantly share code, notes, and snippets.

@tux-00
Last active September 10, 2015 15:19
Show Gist options
  • Save tux-00/9508092 to your computer and use it in GitHub Desktop.
Save tux-00/9508092 to your computer and use it in GitHub Desktop.
Format bytes to kb Mb Gb or Tb automaticaly.
<?php
function formatBytes($size, $precision = 2)
{
if($size== 0) {
return 0;
}
$base = log($size) / log(1024);
$suffixes = array('o', 'kb', 'Mb', 'Gb', 'Tb');
return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment