zefie icon

Future-proof (for a good while) byte size to string function

zefie | PRO | 08/17/18 04:33:53 PM UTC | 0 ⭐ | 789 👁️ | Never ⏰ | []
PHP |

440 B

|

None

|

0 👍

/

0 👎

function friendlyBytes($size,$pre = 2) {
        $sizes = array('B','KB','MB','GB','TB','PB','EB','ZB','YB');
        for ($i = (count($sizes)); $i > 0; $i--) {
                if ($i > 0) {
                        $exp = pow(1024,$i);
                        if ($size > $exp) {
                                return round(($size / $exp),$pre).$sizes[$i];
                        }
                }
        }
        return $size."B";
}

Comments