01572: use of SI units

Summary: use of SI units
Created: 2026-10-12 18:42
Status: Open
Category: Suggestion
From: simon
Assigned:
Priority: 1
Version: latest
OS:

Description: In these functions SI units (e.g. base 1000: k, M, G, T, P, E, Z) are conflated with IEC binary multiples (e.g. base 1024: Ki, Mi, Gi, Ti, Pi, Ei)

## FileSizeCompact outputs a human readable file size
## with an appropriate suffix.
## Note: unreliable filemtime()/stat() over 2GB @ 32bit
function FileSizeCompact($n, $precision=1, $base=1024) {
  if ((float)$n<=0) return '0b';
  $units = 'bkMGTPEZY';
  $b = log((float)$n, $base);
  $fb = (int)floor($b);
  if (!isset($units[$fb])) {
    $fb = strlen($units)-1;
  }
  $u = $units[$fb];
  return round(pow($base,$b-$fb),$precision).$u;
}
function FileSizeFromCompact($x, $base=1024) {
  $units = 'bkmgtpezy';
  $f = floatval($x);
  if (preg_match("/[$units]/", strtolower($x), $m)) {
    $pos = strpos($units, $m[0]);
    $f *= pow($base, $pos);
  }
  return round($f);
}

Modern usage is to describe storage in SI units (base 1000), e.g. 123KB (123 kilo bytes), or 987Gb (987 giga bits)

This suggestion is to update the use of units in PmWiki to SI units

simon

These are helper functions only called from upload.php to report if PHP limits are lower than the PmWiki upload size limits. The PHP limits are in binary units, so the same are used here if you had to edit your php.ini file. The helper functions accept a second parameter with the base: your custom functions can call them with base 1000 if you want decimal units. See also PITS:01499. --Petko

thanks, it seems PHP allows shortcuts for byte values, including K (kilo), M (mega) and G (giga), other wise integers for size are in bytes.
Apparently ini_get() returns the exact string stored in the php.ini file and NOT its int equivalent. ini_parse_quantity() returns interpreted size from ini shorthand syntax:

a number followed by an optional multiplier: k/K (1024), m/M (1048576), g/G (1073741824). The number can be a decimal, hex (prefixed with 0x or 0X), octal (prefixed with 0o, 0O or 0) or binary (prefixed with 0b or 0B)

I do feel that MB and MiB, for example, should be used correctly

simon