<?php
function fm_get_filesize($size)
{
$size = (float) $size;
$size = (int) $size;
$units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$power = $size > 0 ? floor(log($size, 1024)) : 0;
return sprintf('%s %s', round($size / pow(1024, $power), 2), $units[$power]);
$i = 0;
while (($size / 1024) > 0.9) {
$size = $size / 1024;
$i++;
}
// Fix 32bit integer overflow
$int_max = defined('PHP_INT_MAX')
? PHP_INT_MAX
: ((strlen(decbin(~0)) === 32) ? 2147483647 : 9223372036854775807);
$size = ($size < 0) ? ($size + (2.0 * ($int_max + 1))) : $size;
return round($size, 2).' '.$units[$i];
}
echo fm_get_filesize(6565655656);
preferences:
31.22 ms | 409 KiB | 5 Q