--- /t3lib/class.t3lib_div.php Revision 7375 +++ /t3lib/class.t3lib_div.php Revision 7375 patched @@ -1253,40 +1253,53 @@ } /** - * 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 = '') { + $defaultFormat = $GLOBALS['TYPO3_CONF_VARS']['SYS']['byteSizeFormat']; + $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($defaultFormat, $defaults)) { + $defaultFormat = 'traditional'; + } + + // Set labels: if (strlen($labels) == 0) { - $labels = ' | K| M| G'; + $labels = $defaults[$defaultFormat]['labels']; } else { - $labels = str_replace('"','',$labels); + $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]; - } + // Determine the exponent for further calculations, we just us a logarithmic scale with the given base + $exponent = floor( + log( + $sizeInBytes, + $defaults[$defaultFormat]['base'] + ) + ); + // Use next higher unit if the size is greater than the threshold (90 % of next unit) + if ($sizeInBytes > (pow($defaults[$defaultFormat]['base'], $exponent+1)*0.9)) { + $exponent++; + } + // Output and round to 2 significant decimals + return @number_format( + // Size in bytes devided by the power of the dertermined exponent gives us the value we want + $sizeInBytes / pow( + $defaults[$defaultFormat]['base'], + $exponent + ), + (($exponent!=0)?2:0) + ) . ' ' . $labelArr[$exponent]; } + /** * Returns microtime input to milliseconds --- /t3lib/config_default.php Revision 7375 +++ /t3lib/config_default.php Revision 7375 patched @@ -71,6 +71,7 @@ 'enable_DLOG' => FALSE, // Whether the developer log is enabled. See constant "TYPO3_DLOG" 'ddmmyy' => 'd-m-y', // Format of Date-Month-Year - see PHP-function date() 'hhmm' => 'H:i', // Format of Hours-Minutes - see PHP-function date() + 'byteSizeFormat' => 'traditional', // Format of filesizes: eg. 15.02 KB, 15.02 KiB, 15.38 kB; ('traditionl', 'iec' or 'si') 'USdateFormat' => FALSE, // Boolean. If true, dates entered in the TCEforms of the backend will be formatted mm-dd-yyyy 'loginCopyrightWarrantyProvider' => '', // String: If you provide warranty for TYPO3 to your customers insert you (company) name here. It will appear in the login-dialog as the warranty provider. (You must also set URL below). 'loginCopyrightWarrantyURL' => '', // String: Add the URL where you explain the extend of the warranty you provide. This URL is displayed in the login dialog as the place where people can learn more about the conditions of your warranty. Must be set (more than 10 chars) in addition with the 'loginCopyrightWarrantyProvider' message.