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
 
?>

3 comments :

Anonymous said...

Hey, Wow all the posts are very informative for the people who visit this site. Good work! We also have a Website. Please feel free to visit our site. Thank you for sharing. Well written article Thank You for Sharing with Us project management training in chennai | project management certification online | project management course online

Unknown said...

Bukan tanpa alasan melainkan karena ada alasan utama yang mengharuskan anda memilihnya yakni karena dengan banyaknya pilihan yang layak
asikqq
http://dewaqqq.club/
http://sumoqq.today/
interqq
pionpoker
bandar ceme
freebet tanpa deposit
paito warna terlengkap
syair sgp

Nikhil said...

An Android app development course builds strong mobile expertise.It covers UI, backend, and deployment.Practice ensures clarity.This Android app development course supports real projects.It is dependable.