Fork me on GitHub


Convert file size denominations in PHP

December 20, 2006 | PHP Scripting

Convert a file size from one measure to another. For example, take a file that is recorded as 512KB and have it converted to .5MB. Yeah, that’s an easy example, but that’s ’cause I was writing it off the top of my head. So, if you need this sort of thing, here’s a function that I thought came out pretty well.

function convert_file_size($unit,$from,$to)
    $sizes = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');

    list($pos1) = array_keys($sizes,strtoupper($from));
    list($pos2) = array_keys($sizes,strtoupper($to));

    $up = $pos1 < $pos2 ? true : false;

    for($i = $pos1; $i != $pos2; ($up ? $i++ : $i--))
        if($up) { $unit = $unit / 1024; }
        else { $unit = $unit * 1024; }

    return $unit;

So, simply use it like:

$kbytes = convert_file_size(2.7,'MB','KB');

Is any of this code particularly special? No. Do I still think its neat? You betcha!