Skip to content

Instantly share code, notes, and snippets.

@Kcko
Last active December 25, 2015 04:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kcko/6918526 to your computer and use it in GitHub Desktop.
Save Kcko/6918526 to your computer and use it in GitHub Desktop.
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];
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment