Is there already a functions or class that can be used to convert bytes to whatever it should be such as KB, MB, GB, etc?
I have been trying to find a conversion method to include with my mod but every one of the method's I have found don't work, either I can't seem to get them to match vB or something.
Here is one I found:
PHP Code:
function ByteSize($bytes)
{
$size = $bytes / 1024;
if($size < 1024)
{
$size = number_format($size, 2);
$size .= ' KB';
}
else
{
if($size / 1024 < 1024)
{
$size = number_format($size / 1024, 2);
$size .= ' MB';
}
else if ($size / 1024 / 1024 < 1024)
{
$size = number_format($size / 1024 / 1024, 2);
$size .= ' GB';
}
}
return $size;
}
// Returns '19.28mb'
print ByteSize('20211982');
Another method I found:
PHP Code:
function formatRawSize($bytes) {
//CHECK TO MAKE SURE A NUMBER WAS SENT
if(!empty($bytes)) {
//SET TEXT TITLES TO SHOW AT EACH LEVEL
$s = array('bytes', 'kb', 'MB', 'GB', 'TB', 'PB');
$e = floor(log($bytes)/log(1024));
//CREATE COMPLETED OUTPUT
$output = sprintf('%.2f '.$s[$e], ($bytes/pow(1024, floor($e))));
//SEND OUTPUT TO BROWSER
return $output;
}
}
Thanks in advance