--- /t3lib/class.t3lib_div.php 2010-02-26 18:40:53 (Revision 7091) +++ /t3lib/class.t3lib_div.php 2010-03-09 00:02:16 @@ -1216,39 +1216,33 @@ } /** - * Formats the input integer $sizeInBytes as bytes/kilobytes/megabytes (-/K/M) + * Formats the input integer $sizeInBytes as bytes/kilobytes/megabytes (-/K/M) and beyond (theoretically infinite) * Usage: 53 * - * @param integer Number of bytes to format. - * @param string Labels for bytes, kilo, mega and giga separated by vertical bar (|) and possibly encapsulated in "". Eg: " | K| M| G" (which is the default value) - * @return string Formatted representation of the byte number, for output. + * @param integer Number of bytes to format. + * @param string Labels for bytes, kilo, mega and giga separated by vertical bar (|) and possibly encapsulated in "". Eg: " B| KB| MB| GB | TB" (default value) + * @return string Formatted representation of the byte number, for output (label is suffixed). */ - public static function formatSize($sizeInBytes,$labels='') { - - // Set labels: + public static function formatSize($sizeInBytes,$labels='') { + $default_format = 'traditional'; // should be moved to config_default.php, like $TYPO3_CONF_VARS['SYS']['ddmmyy'] and ['hhmm'] + $defaults = array( + 'traditional' => array('base' => 1024, 'labels' => ' B| KB| MB| GB | TB'), + 'iec' => array('base' => 1024, 'labels' => ' B| KiB| MiB| GiB | TiB'), + 'si' => array('base' => 1000, 'labels' => ' B| kB| MB| GB | TB') + ); + if (!array_key_exists($default_format, $defaults)) { $default_format = 'traditional'; } + + // Set labels: if (strlen($labels) == 0) { - $labels = ' | K| M| G'; + $labels = $defaults[$default_format]['labels']; } else { $labels = str_replace('"','',$labels); } $labelArr = explode('|',$labels); - - // Find size: - if ($sizeInBytes>900) { - if ($sizeInBytes>900000000) { // GB - $val = $sizeInBytes/(1024*1024*1024); - return number_format($val, (($val<20)?1:0), '.', '').$labelArr[3]; - } - elseif ($sizeInBytes>900000) { // MB - $val = $sizeInBytes/(1024*1024); - return number_format($val, (($val<20)?1:0), '.', '').$labelArr[2]; - } else { // KB - $val = $sizeInBytes/(1024); - return number_format($val, (($val<20)?1:0), '.', '').$labelArr[1]; - } - } else { // Bytes - return $sizeInBytes.$labelArr[0]; - } + + $exponent = floor(log($sizeInBytes, $defaults[$default_format]['base'])); // determine the exponent for further calculations + if ($sizeInBytes > (pow($defaults[$default_format]['base'], $exponent+1)*0.9)) { $exponent++; } // use next higher unit if the size is greater than the threshold (90 % of next unit) + return @number_format($sizeInBytes/pow($defaults[$default_format]['base'], $exponent), (($exponent!=0)?2:0)) . ' ' . $labelArr[$exponent]; // output and round to 2 significant decimals } /**