Kcko
10/10/2013 - 1:42 PM

PHP: Readable filesize

PHP: Readable filesize

<? 

function HumanReadableFilesize($size) {
// Adapted from: http://www.php.net/manual/en/function.filesize.php
$mod = 1024;
$units = explode(' ','B KB MB GB TB PB');
for ($i = 0; $size > $mod; $i++) {
$size /= $mod;
}
return round($size, 2) . ' ' . $units[$i];
}

function fsize($file) {
$a = array("B", "KB", "MB", "GB", "TB", "PB");
$pos = 0;
$size = filesize($file);
while ($size >= 1024) {$size /= 1024;$pos++;}
return round($size,2)." ".$a[$pos];
}

?>