Created
July 2, 2020 01:12
-
-
Save cp6/da66f142efaed74dd7f16ae6f586cf19 to your computer and use it in GitHub Desktop.
PHP convert and format bytes function
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
<?php | |
function convertBytes(int $bytes, string $convert_to = 'KB', bool $format = true, int $decimals = 2): float | |
{ | |
if ($convert_to == 'KB') { | |
$value = ($bytes / 1024); | |
} elseif ($convert_to == 'MB') { | |
$value = ($bytes / 1048576); | |
} elseif ($convert_to == 'GB') { | |
$value = ($bytes / 1073741824); | |
} elseif ($convert_to == 'TB') { | |
$value = ($bytes / 1099511627776); | |
} else { | |
$value = $bytes; | |
} | |
if ($format) $value = number_format($value, $decimals); | |
return $value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment