roundbytes.phps

<?

function roundbytes($bytes) {
  # Scale:
  # B = byte        KB = kilobyte  MB = megabyte   GB = gigabyte
  # TB = terabyte   PB = petabyte  EB = exabyte    ZB = zetabyte
  # YB = yottabyte  NB = nonabyte  DB = doggabyte
  $suffix = array("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", "NB", "DB");
  $pos = 0;
  while ($bytes >= 1024) {
    if ($pos == 10) { break; }
    $bytes /= 1024;
    $pos++;
  }
  $result=round($bytes,2)."".$suffix[$pos];
  return $result;
}

?>