PHP function that returns file size of a given file as a string indicating size in kilobytes or megabytes, depending on whether the file is bigger than one megabyte.

function filesizeFormatted($filename) {
  $val=filesize($filename)/1024;

  if($val>1024) return "".number_format($val/1024,1,"."," ")." MB";
  else return "".number_format($val,1,"."," ")." kb";
}

1 Comment »

There is one comment to "Code (PHP): filesizeFormatted()". You may leave your own comment.
1. Ryan the Fish Scale, October 31st, 2006 at 22:53

Oh weird I guess it doesnt like the lessthan symbol. I’ll try again.

function formatsize($size)
{
$kb = 1024; // Kilobyte
$mb = 1024 * $kb; // Megabyte
$gb = 1024 * $mb; // Gigabyte
$tb = 1024 * $gb; // Terabyte
if($size == 0) { return 'empty'; }
if($size < $kb) { return $size.' bytes'; }
else if($size < $mb) { return round($size/$kb,2).' kb'; }
else if($size < $gb) { return round($size/$mb,2).' mb'; }
else if($size < $tb) { return round($size/$gb,2).' gb'; }
else { return round($size/$tb,2).' tb'; }
}

Comment on this entry

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">