Convert byte count
By: Daniel

So you have the byte count of a file but you don’t have the time to try converting it in your head to KB or MB. Using the following code this is easy.

<?php
function convertbytes($data) {
if($data < 1024) {
return $data." bytes";
} elseif($data < 1024000) {
return round(($data / 1024), 2)." k";
} else {
return round(($data / 1048576), 2)." MB";
}
}
?>

Just supply the filesize in the function and it will return the filesize in an easier to readable format.

An example of this could be:

<?php
echo convertbytes(filesize('/path/to/your/file.php'));
echo convertbytes('10000'); // would output 9.77 k
?>

Enjoy!

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 4.00 out of 5)
Loading ... Loading ...

Leave a Reply

geovisitors