Die Methode „formatBytes“ rechnet die übergebenen Bytes in die nächste passende Einheit um (z.B. 1024 Byte > 1 kB).
Code:
function formatBytes($iBytes)
{
$aUnits = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB');
$sResult = "0.00 B";
if($iBytes > 0)
{
$fResult = log($iBytes) / log(1024);
$sResult = round(pow(1024, $fResult - ($fTmp = floor($fResult))), 2)." ".$aUnits[$fTmp];
}
return $sResult;
}
{
$aUnits = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB');
$sResult = "0.00 B";
if($iBytes > 0)
{
$fResult = log($iBytes) / log(1024);
$sResult = round(pow(1024, $fResult - ($fTmp = floor($fResult))), 2)." ".$aUnits[$fTmp];
}
return $sResult;
}
Beispiel:
echo formatBytes(1024); //1 kB
echo formatBytes(515152222); //491.29 MB
echo formatBytes(85939939394895); //78.16 TB
echo formatBytes(515152222); //491.29 MB
echo formatBytes(85939939394895); //78.16 TB
Nice, usefull function. 🙂
It would be nice to have it split into a few more lines, to better understand what is going on, and perhaps a few comments.