format_size

Versions
4.6 – 5
format_size($size)
6 – 7
format_size($size, $langcode = NULL)

Generate 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

▾ 15 functions call format_size()

color_scheme_form_submit in modules/color/color.module
Submit handler for color change form.
file_ajax_progress in modules/file/file.module
Menu callback for upload progress.
file_ajax_upload in modules/file/file.module
Menu callback; Shared AJAX callback for file uploads and deletions.
file_field_instance_settings_form in modules/file/file.field.inc
Implement hook_field_instance_settings_form().
file_save_upload in includes/file.inc
Saves a file upload to a new location.
file_validate_size in includes/file.inc
Check that the file's size is below certain limits.
system_tokens in modules/system/system.tokens.inc
Implement hook_tokens().
theme_field_formatter_file_table in modules/file/file.field.inc
Theme function for the 'table' formatter.
theme_file_upload_help in modules/file/file.field.inc
Generate help text based on upload validators.
theme_file_widget in modules/file/file.field.inc
Theme an individual file upload widget.
theme_image_widget in modules/image/image.field.inc
Theme the display of the image field widget.
theme_upload_attachments in modules/upload/upload.module
Displays file attachments in table
upload_admin_settings in modules/upload/upload.admin.inc
Menu callback for the upload settings form.
upload_admin_settings_validate in modules/upload/upload.admin.inc
Form API callback to validate the upload settings form.
_upload_form in modules/upload/upload.module

Code

includes/common.inc, line 2185

<?php
function format_size($size, $langcode = NULL) {
  if ($size < DRUPAL_KILOBYTE) {
    return format_plural($size, '1 byte', '@count bytes', array(), array('langcode' => $langcode));
  }
  else {
    $size = $size / DRUPAL_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)),
    );
    foreach ($units as $unit) {
      if (round($size, 2) >= DRUPAL_KILOBYTE) {
        $size = $size / DRUPAL_KILOBYTE;
      }
      else {
        break;
      }
    }
    return str_replace('@size', round($size, 2), $unit);
  }
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.