Friendly file size for a file using php code

in PHP Code / 1 Comment

Hi friends, In this post i would like to explain about get the friendly file size using simple php code.

PHP Code

following code is very simple to get the friendly file size format using php code.

function frndlyfilesize($filesize){

$decr = 1024;
$step = 0;
$prefix = array('Byte','KB','MB','GB','TB','PB');

while(($fileName / $decr) > 0.9){
$filesize = $filesize / $decr;
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 ……

With more than 9 years experience in PHP and Founder of Anil Labs, a blog for PHP and related posts. contributed posts regarding CodeIgniter, CakePHP and Learn PHP online ... Contact : [email protected]

Discussion 1 Comment

  1. Ruel

    I think it should be
    while(($filesize / $decr) > 0.9)

    and NOT
    while(($fileName / $decr) > 0.9)

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>