Have an account? Sign in
Login  Register  Facebook
This Page is Under Construction! - If You Want To Help Please Send your CV - Advanced Web Core (BETA)
[Edit] Convert octets to Ko, Mo, Go
When displaying a filesize, it is way much nicer to display “1 Mo” instead of “1048576 octets”. This very handy code snippet take a filesize in octets as a parameter, and will return the filesize converted to Ko, Mo or Go.

Usage

Simply call the function with your filesize as a parameter:
echo GetSizeName(21354685545);
Save 44,364 octets
function GetSizeName($octet){
    $unite = array(' octet',' Ko',' Mo',' Go');

    if ($octet < 1000) {
        return $octet.$unite[0];
    } else {
        if ($octet < 1000000) {
            $ko = round($octet/1024,2);
            return $ko.$unite[1];
        } else {
            if ($octet < 1000000000) {
                $mo = round($octet/(1024*1024),2);
                return $mo.$unite[2];
            } else {
                $go = round($octet/(1024*1024*1024),2);
                return $go.$unite[3];
            }
        }
    }
}