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";
}





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'; }
}