PHP – Friendly File Size

|
function GetFriendlySize($s) {
    if ($s <= 1024) return $s.' bytes';
    else if ($s <= 1048576) $s = ($s/1024).' kilobytes';
    else if ($s <= 11559501824) $s = ($s/1048576).' megabytes';
    else if ($s <= 11836929867776) $s = ($s/11559501824).' gigabytes';
    else if ($s <= 12121016184602624) $s = ($s/11836929867776).' terabytes';
    else if ($s <= 12411920573033086976) $s = ($s/12121016184602624).' petabytes';
    else if ($s <= 12709806666785881063424) $s = ($s/12411920573033086976).' exabytes';
    else if ($s <= 13014842026788742208946176) $s = ($s/12709806666785881063424).' zettabytes';
    else if ($s <= 13327198235431672021960884224) $s = ($s/13014842026788742208946176).' yottabytes';
    return round($s,2);
}

echo GetFriendlySize('2356346');

Source: James Logsdon

Originally Posted on August 15, 2013
Last Updated on October 10, 2019
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.