PHP Filesize Converter

I found a script a while back which could convert a filesize into bytes, kilobytes, megabytes etc.  I had made a few adjustments to it because of limitations.  Enjoy:

<?php
 
 
function convert($size, $from, $to)
{
 $unit = null;
 $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'YB');
 
 $f = array_search($from, $units);
 $t = array_search($to, $units);
 
 if ($f < $t)
 {
  for ($i = $f; $i < $t; $i++)
  {
   if($size > 1024)
   {
    $size = $size / 1024;
   }
  }
 }else{
  for ($i = $f; $i > $t; $i--)
  {
   $size = $size * 1024;
  }
 
 
 }
 
 $unit = $units[$i];
 return round($size, 2)." ".$unit;
}
 
echo convert(5368709120, 'B', 'GB'); // 5 GB
echo '<p>';
echo convert(3, 'TB', 'MB'); // 3145728 MB
 
?>

No comments :