file size in human readable format

From WebHostingNeeds.com
Jump to: navigation, search

This function will format file size in bytes to human readable format, that is KB, MB, GB etc...

function file_size_human_readable($size) {
  switch ($size) {
    case ($size>=1048576): $size = round($size/1048576) . " MB"; break;
    case ($size>=1024);    $size = round($size/1024) . " KB"; break;
    default:               $size = $size . " bytes"; break;
  }
  return $size;
}

Here is some other use full codes

if ($filesize < 500)
{
$filesize = "$grootte b";
}
elseif ($filesize <((1024 * 1024)/2))
{
$filesize = round ($filesize / 1024,1);
$filesize = "$filesize Kb";
}
else
{
$filesize = round (($filesize / 1024 / 1024),2);
$filesize = "$filesize Mb";
}