Skip to content

Instantly share code, notes, and snippets.

@svlasov
Created March 27, 2012 13:22
Show Gist options
  • Save svlasov/2215819 to your computer and use it in GitHub Desktop.
Save svlasov/2215819 to your computer and use it in GitHub Desktop.
Humanize file size
<?php
function humanizeFileSize($pathToFile)
{
if (is_readable($pathToFile)) {
$size = filesize($pathToFile);
if ($size < 1024) {
return $size .'B';
} elseif ($size < 1048576) {
return round($size / 1024, 2) .'Kb';
} elseif ($size < 1073741824) {
return round($size / 1048576, 2) . 'Mb';
} elseif ($size < 1099511627776) {
return round($size / 1073741824, 2) . 'Gb';
} elseif ($size < 1125899906842624) {
return round($size / 1099511627776, 2) .'Tb';
} elseif ($size < 1152921504606846976) {
return round($size / 1125899906842624, 2) .'Pb';
} elseif ($size < 1180591620717411303424) {
return round($size / 1152921504606846976, 2) .'Eb';
} elseif ($size < 1208925819614629174706176) {
return round($size / 1180591620717411303424, 2) .'Zb';
} else {
return round($size / 1208925819614629174706176, 2) .'Yb';
}
} else {
return NULL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment