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.

// 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)];


Sign in to comment.
bone282   -  May 16, 2009


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


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


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

^Neptune   -  May 16, 2009

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

bone282   -  May 16, 2009

a clever use of math there.

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().

X-FILE   -  Sep 03, 2004

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

Hawkee   -  Sep 02, 2004

It\'s in the PHP code snippet section

Lostgamer   -  Sep 02, 2004

looks like Php code

Hawkee   -  Sep 02, 2004

what do you use this for?

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.