Hi everyone,
I was working on a project where there were a lot of file operations and so we needed to format size a number of times. For this I added a formatter method called formatSize to the CFormatter class. Most of the PHP frameworks have this function. So I thought it should be part of Yii too. So if you people are planning to include formatSize in Yii, this is what I am using:
/**
* @var array the format used to format size (bytes). Two elements may be specified: "base" and "decimals".
* They correspond to the base at which KiloByte is calculated (1000 or 1024) bytes per KiloByte and
* the number of digits after decimal point.
*/
public $sizeFormat=array('base'=>1024,'decimals'=>2);
/**
* Formats the value as a size in human readable form.
* @params integer value to be formatted
* @return string the formatted result
*/
public function formatSize($value)
{
$units=array('B','KB','MB','GB','TB');
$base = $this->sizeFormat['base'];
for($i=0; $base<=$value; $i++) $value=$value/$base;
return round($value, $this->sizeFormat['decimals']).$units[$i];
}
Thanks.
Enjoy