1function formatBytes($bytes, $precision = 2) {
2 $units = array('B', 'KB', 'MB', 'GB', 'TB');
3
4 $bytes = max($bytes, 0);
5 $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
6 $pow = min($pow, count($units) - 1);
7
8 // Uncomment one of the following alternatives
9 // $bytes /= pow(1024, $pow);
10 // $bytes /= (1 << (10 * $pow));
11
12 return round($bytes, $precision) . ' ' . $units[$pow];
13}