Your Ad Here

Home » PHP Code »

1

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 :

1
2
$ filesize = filesize("test.jpg");
$newSize = frndlyfilesize($filesize);

 

Expected output like the format :

 

5 MB  or 5 KB or 5 GB ……


You may like these posts

    Facebook type notifications Using PHP and  JquerySimple Steps to Build a Custom CMS application Using PHPHow to highlight the source code in our php application?Unzip file using php code

 

1 Comment

  1. Ruel says:

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

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

Leave a Reply