Filesize Units with PHP

From Birnam Designs Wiki

Jump to: navigation, search

A quick way to convert filesize to units with PHP:

<?php
    function byteConvert($bytes)
    {
        $s = array('B', 'Kb', 'MB', 'GB', 'TB', 'PB');
        $e = floor(log($bytes)/log(1024));
 
        return sprintf('%.2f '.$s[$e], ($bytes/pow(1024, floor($e))));
    }
?>

Thanks olafurw at gmail.com!

Share This!
This page was last modified on 11 February 2010, at 20:21. This page has been accessed 428 times.