Skip to content

Instantly share code, notes, and snippets.

@polonskiy
Created January 9, 2014 13:30
Show Gist options
  • Save polonskiy/8334122 to your computer and use it in GitHub Desktop.
Save polonskiy/8334122 to your computer and use it in GitHub Desktop.
Bytes to KB/MB/GB/TB converter
<?php
function byte2($bytes) {
$size = array('B','KB','MB','GB','TB');
$factor = floor(log($bytes, 1024));
$format = $factor > 2 ? '%.1F %s' : '%u %s';
return sprintf($format, $bytes / pow(1024, $factor), $size[$factor]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment