Project

General

Profile

Feature #22175 » 13645_v2.diff

Administrator Admin, 2010-02-24 21:42

View differences:

class.t3lib_div.patched.php Wed Feb 24 21:40:37 2010
}
/**
* 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| KiB| MiB| GiB | TiB" (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 = 'iec'; // should be moved next to config_default.php, like $TYPO3_CONF_VARS['SYS']['ddmmyy'] and ['hhmm']
$defaults = array(
'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 = 'iec'; }
// 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];
// done via for loop - for infinite expandability; old version took lot use of if constructs
for ($i = count($labelArr); $i >= 0; $i--) {
$calcbase = pow($defaults[$default_format]['base'], $i);
if ($sizeInBytes > $calcbase*0.9) {
return number_format($sizeInBytes/$calcbase, (($i!=0)?2:0), '.', '') . $labelArr[$i];
}
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];
}
}
(2-2/2)