Posted by: Anil Kumar Panigrahi in: ● March 16, 2010
Hi friends,
following code is very simple to get the friendly file size format using php code.
function frndlyfilesize($filesize){
if(is_numeric($filesize)){
$decr = 1024;
$step = 0;
$prefix = array(‘Byte’,'KB’,'MB’,'GB’,'TB’,'PB’);while(($fileName / $decr) > 0.9){
$filesize = $filesize / $decr;
$step++;
}
return round($filesize,2).’ ‘.$prefix[$step];
} else {return ‘Nothing’;
}
}
To call that function :
$ filesize = filesize(“test.jpg”);
$newSize = frndlyfilesize($filesize);
Expected output like the format :
5 MB or 5 KB or 5 GB ……
Comments