Created
March 27, 2012 13:22
-
-
Save svlasov/2215819 to your computer and use it in GitHub Desktop.
Humanize file size
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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