1 common.inc format_size($size, $langcode = NULL)

Generates a string representation for the given byte count.

Parameters

$size: A size in bytes.

$langcode: Optional language code to translate to a language other than what is used to display the page.

Return value

A translated string representation of the size.:

Related topics

File

core/includes/common.inc, line 2371
Common functions that many Backdrop modules will need to reference.

Code

function format_size($size, $langcode = NULL) {
  if ($size < BACKDROP_KILOBYTE) {
    return format_plural($size, '1 byte', '@count bytes', array(), array('langcode' => $langcode));
  }
  else {
    $size = $size / BACKDROP_KILOBYTE; // Convert bytes to kilobytes.
    $units = array(
      t('@size KB', array(), array('langcode' => $langcode)),
      t('@size MB', array(), array('langcode' => $langcode)),
      t('@size GB', array(), array('langcode' => $langcode)),
      t('@size TB', array(), array('langcode' => $langcode)),
      t('@size PB', array(), array('langcode' => $langcode)),
      t('@size EB', array(), array('langcode' => $langcode)),
      t('@size ZB', array(), array('langcode' => $langcode)),
      t('@size YB', array(), array('langcode' => $langcode)),
    );
    $unit = reset($units);
    foreach ($units as $unit) {
      if (round($size, 2) >= BACKDROP_KILOBYTE) {
        $size = $size / BACKDROP_KILOBYTE;
      }
      else {
        break;
      }
    }
    return str_replace('@size', round($size, 2), $unit);
  }
}