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 Votes | Average: 4 out of 51 Votes | Average: 4 out of 51 Votes | Average: 4 out of 51 Votes | Average: 4 out of 51 Votes | Average: 4 out of 5 (1 votes, average: 4 out of 5)
Loading ... Loading ...
del.icio.us:Convert byte count digg:Convert byte count spurl:Convert byte count wists:Convert byte count simpy:Convert byte count newsvine:Convert byte count blinklist:Convert byte count furl:Convert byte count reddit:Convert byte count fark:Convert byte count blogmarks:Convert byte count Y!:Convert byte count smarking:Convert byte count magnolia:Convert byte count segnalo:Convert byte count

Leave a Reply

eXTReMe Tracker
geovisitors