Skip to content

Instantly share code, notes, and snippets.

@egulhan
Created June 30, 2014 11:59
Show Gist options
  • Save egulhan/61f5e4360efdb7fa1473 to your computer and use it in GitHub Desktop.
Save egulhan/61f5e4360efdb7fa1473 to your computer and use it in GitHub Desktop.
Converts bytes into human readable file size
@source: http://www.php.net/manual/en/function.filesize.php#112996
<?php
/**
* Converts bytes into human readable file size.
*
* @param string $bytes
* @return string human readable file size (2,87 Мб)
* @author Mogilev Arseny
*/
function FileSizeConvert($bytes)
{
$bytes = floatval($bytes);
$arBytes = array(
0 => array(
"UNIT" => "TB",
"VALUE" => pow(1024, 4)
),
1 => array(
"UNIT" => "GB",
"VALUE" => pow(1024, 3)
),
2 => array(
"UNIT" => "MB",
"VALUE" => pow(1024, 2)
),
3 => array(
"UNIT" => "KB",
"VALUE" => 1024
),
4 => array(
"UNIT" => "B",
"VALUE" => 1
),
);
foreach($arBytes as $arItem)
{
if($bytes >= $arItem["VALUE"])
{
$result = $bytes / $arItem["VALUE"];
$result = str_replace(".", "," , strval(round($result, 2)))." ".$arItem["UNIT"];
break;
}
}
return $result;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment