Tuesday 2 June 2015

Make Nice File Size function to return a formatted filesize (b, kb, mb, gb)

<?php
function mk_nice_filesize($fpath) {
 $d = filesize($fpath);
 $text = "B";
 
 if ($d > 1023) {
  $d = round(($d/1024),2);
  $text = "KB";
  
  if ($d > 1023) {
   $d = round(($d/1024),2);
   $text = "MB";
   
   if ($d > 1023) {
    $d = round(($d/1024),2);
    $text = "GB";
   }
  }
 }
 
 return $d . " " . $text;
}
?>
 

Usage

Just pass the full path of the file to this function to get a formatted filesize.
 

0 comments:

Post a Comment