PHP

Function to Display User Friendly File Size

I thought I would just post this very handy little function that displays a file size in a readable fashion:

function display_filesize($filesize){

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

 while(($filesize / $decr) > 0.9){
 $filesize = $filesize / $decr;
 $step++;
 }
 return round($filesize,2).' '.$prefix[$step];
 } else {

 return 'NaN';
 }
}

typical usage of this function is:

$newSize = display_filesize($filesize);

The typical output is: 2KB, 2MB, 2GB and so on

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s