Return to Snippet

Revision: 39523
at January 18, 2011 04:19 by kendsnyder


Initial Code
function bytesToText($bytes, $precision = 2, $units = null) {
  static $suffix = array('b', 'KB', 'MB', 'GB', 'TB');
  if ($units) {
    $power = array_search($units, $suffix);
    $i = $power;
    while ($i-- > 0) {
      $bytes /= 1024;
    }

  } else {
	  $bytes = (float) $bytes;
	  $power = 0;
	  while ($bytes >= 1000) {
	    $bytes /= 1024;
	    $power++;
	  }
  }
  if ($power == 0) {
    $precision = 0;
  }
  return number_format($bytes, $precision) . $suffix[$power];
}

Initial URL
bytes-to-text

Initial Description


Initial Title
Convert bytes to text

Initial Tags
convert

Initial Language
PHP