Feature #22175 » 13645.diff
class.t3lib_div.php Tue Feb 23 12:36:50 2010 | ||
---|---|---|
}
|
||
|
||
/**
|
||
* Formats the input integer $sizeInBytes as bytes/kilobytes/megabytes (-/K/M) and beyond (theoretically infinite)
|
||
* Formats the input integer $sizeInBytes as bytes/kilobytes/megabytes (-/K/M)
|
||
* 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: " B| KiB| MiB| GiB | TiB" (default value)
|
||
* @return string Formatted representation of the byte number, for output (label is suffixed).
|
||
* @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.
|
||
*/
|
||
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:
|
||
public static function formatSize($sizeInBytes,$labels='') {
|
||
|
||
// Set labels:
|
||
if (strlen($labels) == 0) {
|
||
$labels = $defaults[$default_format]['labels'];
|
||
$labels = ' | K| M| G';
|
||
} else {
|
||
$labels = str_replace('"','',$labels);
|
||
}
|
||
$labelArr = explode('|',$labels);
|
||
|
||
// 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];
|
||
|
||
// 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];
|
||
}
|
||
}
|
||
|