ANIL KUMAR PANIGRAHI 's Blog

Friendly file size for a file using php code

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

Share on Twitter

No Comments to "Friendly file size for a file using php code"

Write Comment

About Me

  Anil Kumar Panigrahi
  Software Engineer (PHP)
  Blogger & Founder of Anil Labs
  Baruva, India

You can share with me . . .

My Own Contributions

1) First contribution for codeigniter framework ( multi language support using google translate API ). we can get files from http://github.com/nyros/codeigniter_multilanguage


2) Audio and Video Streaming using FFMpeg and PHP -complete doc file : http://www.docstoc.com/docs/8300349/Audio-and-Video-Streaming

Twitter Updates

Follow me @anil2u