- floor: documentation ( source)
- pow: documentation ( source)
- sprintf: documentation ( source)
<?php
/**
* convertBytes
*
* Convert bytes into human readable form with rounding
*
* @name convertBytes
* @access public
* @param integer $bytes
* @param integer $decimals = 2
* @desc Convert bytes into human readable form with rounding
* @return string
*/
function convertBytes($bytes, $decimals = 2)
{
$suffixes = 'BKMGTP';
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$suffixes[$factor];
}
echo convertBytes(1500)."\n";
echo convertBytes(1500*1000)."\n";
echo convertBytes(1500*1000*1000)."\n";
echo convertBytes(1500*1000*1000*1000)."\n";
echo convertBytes(1500*1000*1000*1000*1000)."\n";