Created
October 6, 2017 00:15
-
-
Save rwakos/18136f64bfba430dd8221161b4395457 to your computer and use it in GitHub Desktop.
PHP - Get Filesize in different units, depending on its size
This file contains 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
function getMaxUnitFromBytes($bytes) | |
{ | |
if ($bytes < 1024) { | |
return $bytes . ' bytes'; | |
} elseif (($bytes / 1024) < 1024) { | |
return ceil($bytes / 1024) . ' Kb'; | |
} elseif (($bytes / (1024 * 1024)) < 1024) { | |
return ceil($bytes / (1024 * 1024)) . ' Mb'; | |
} elseif (($bytes / (1024 * 1024 * 1024)) < 1024) { | |
return ceil($bytes / (1024 * 1024 * 1024)) . ' Gb'; | |
} else { | |
return ceil($bytes / (1024 * 1024 * 1024 * 1024)) . ' Tb'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment