Advertisement
ryanharne

PHP Bytes Converters

Sep 24th, 2013
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.81 KB | None | 0 0
  1. function formatBytes($bytes, $precision = 2)
  2. {
  3.     $base = log($bytes) / log(1024);
  4.     $suffixes = array('b', 'kb', 'Mb', 'Gb', 'Tb');  
  5.     return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
  6. }
  7.  
  8. function formatSizeUnits($bytes)
  9. {
  10.     if ($bytes >= 1073741824)
  11.     {
  12.         $bytes = number_format($bytes / 1073741824, 2) . ' GB';
  13.     }
  14.     elseif ($bytes >= 1048576)
  15.     {
  16.         $bytes = number_format($bytes / 1048576, 2) . ' MB';
  17.     }
  18.     elseif ($bytes >= 1024)
  19.     {
  20.         $bytes = number_format($bytes / 1024, 2) . ' KB';
  21.     }
  22.     elseif ($bytes > 1)
  23.     {
  24.         $bytes = $bytes . ' bytes';
  25.     }
  26.     elseif ($bytes == 1)
  27.     {
  28.         $bytes = $bytes . ' byte';
  29.     }
  30.     else
  31.     {
  32.         $bytes = '0 bytes';
  33.     }
  34.  
  35.     return $bytes;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement