Home > PHP > PHP Code > Friendly file size for a file using php code

Friendly file size for a file using php code

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 ……

You may also like
Multi-language support for web application with MySql and PHP
How to add or remove file fields using jquery
How to calculate statistics in daily, weekly , monthly and yearly using php
How to rectify json_encode returns null in php web services

1 Response

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.