Skip to content

Instantly share code, notes, and snippets.

@rwakos
Created October 6, 2017 00:15
Show Gist options
  • Save rwakos/18136f64bfba430dd8221161b4395457 to your computer and use it in GitHub Desktop.
Save rwakos/18136f64bfba430dd8221161b4395457 to your computer and use it in GitHub Desktop.
PHP - Get Filesize in different units, depending on its size
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