Return to Snippet

Revision: 7076
at July 4, 2008 06:35 by gbot


Updated Code
/**
 * Get the human-readable size for an amount of bytes
 * @param int  $size      : the number of bytes to be converted
 * @param int $precision  : number of decimal places to round to;
 *                          optional - defaults to 2
 * @param bool $long_name : whether or not the returned size tag should
 *                          be unabbreviated (ie "Gigabytes" or "GB");
 *                          optional - defaults to true
 * @param bool $real_size : whether or not to use the real (base 1024)
 *                          or commercial (base 1000) size;
 *                          optional - defaults to true
 * @return string         : the converted size
 */
function get_size($size,$precision=2,$long_name=true,$real_size=true) {
   $base=$real_size?1024:1000;
   $pos=0;
   while ($size>$base) {
      $size/=$base;
      $pos++;
   }
   $prefix=get_size_prefix($pos);
   $size_name=$long_name?$prefix."bytes":$prefix[0].'B';
   return round($size,$precision).' '.ucfirst($size_name);
}
/**
 * @param int $pos : the distence along the metric scale relitive to 0
 * @return string  : the prefix
 */
function get_size_prefix($pos) {
   switch ($pos) {
      case 00: return "";
      case 01: return "kilo";
      case 02: return "mega";
      case 03: return "giga";
      case 04: return "tera";
      case 05: return "peta";
      case 06: return "exa";
      case 07: return "zetta";
      case 08: return "yotta";
      case 09: return "xenna";
      case 10: return "w-";
      case 11: return "vendeka";
      case 12: return "u-";
      default: return "?-";
   }
}

Revision: 7075
at July 4, 2008 06:32 by gbot


Initial Code
/** * Get the human-readable size for an amount of bytes * @param int $size : the number of bytes to be converted * @param int $precision : number of decimal places to round to; * optional - defaults to 2 * @param bool $long_name : whether or not the returned size tag should * be unabbreviated (ie "Gigabytes" or "GB"); * optional - defaults to true * @param bool $real_size : whether or not to use the real (base 1024) * or commercial (base 1000) size; * optional - defaults to true * @return string : the converted size */ function get_size($size,$precision=2,$long_name=true,$real_size=true) { $base=$real_size?1024:1000; $pos=0; while ($size>$base) { $size/=$base; $pos++; } $prefix=get_size_prefix($pos); $size_name=$long_name?$prefix."bytes":$prefix[0].'B'; return round($size,$precision).' '.ucfirst($size_name); } /** * @param int $pos : the distence along the metric scale relitive to 0 * @return string : the prefix */ function get_size_prefix($pos) { switch ($pos) { case 00: return ""; case 01: return "kilo"; case 02: return "mega"; case 03: return "giga"; case 04: return "tera"; case 05: return "peta"; case 06: return "exa"; case 07: return "zetta"; case 08: return "yotta"; case 09: return "xenna"; case 10: return "w-"; case 11: return "vendeka"; case 12: return "u-"; default: return "?-"; } }

Initial URL
http://www.roscripts.com/snippets/show/188

Initial Description

                                

Initial Title
PHP - Size converter

Initial Tags
php, file

Initial Language
PHP