File Size using Binary Prefixes

By tye on Sep 02, 2004

Returns the size of a file using binary prefixes when given the file size in bytes.

<?php
// fs(size) returns formatted file size string
function fs($fs) {
     $sizes = array('B','KiB','MiB','GiB','TiB');

     $ex = substr(floor(log($fs)/log(2)),0,-1);
     $v = round($fs / pow(2,$ex * 10),2);

     return $v . $sizes[($ex ? $ex : 0)];
}
?>

Comments

Sign in to comment.
bone282   -  May 16, 2009

yes

$file = 'yourfile.txt';
echo count(file($file));

or

$file = file('yourfile.txt');
echo count($file);

or

echo count(file('youfile.txt'));

 Respond  
^Neptune   -  May 16, 2009

Is there a function that returns the number of lines in a text file?

 Respond  
bone282   -  May 16, 2009

a clever use of math there.

 Respond  
tye   -  Sep 07, 2004

It needs floor() to get a whole number from the division. afiak PHP has no operator to do this so I used floor().

 Respond  
X-FILE   -  Sep 03, 2004

you don\'t need to use floor. It Returns the lowest integer value.

 Respond  
Hawkee   -  Sep 02, 2004

It\'s in the PHP code snippet section

 Respond  
Lostgamer   -  Sep 02, 2004

looks like Php code

 Respond  
Hawkee   -  Sep 02, 2004

what do you use this for?

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.