Created
October 13, 2016 04:35
-
-
Save rotexdegba/f53462a49269d36764d85dad02026dc4 to your computer and use it in GitHub Desktop.
Format Bytes to Readable Unit
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 formatSizeUnits($bytes) | |
{ | |
if ($bytes >= 1073741824) | |
{ | |
$bytes = number_format($bytes / 1073741824, 2) . ' GB'; | |
} | |
elseif ($bytes >= 1048576) | |
{ | |
$bytes = number_format($bytes / 1048576, 2) . ' MB'; | |
} | |
elseif ($bytes >= 1024) | |
{ | |
$bytes = number_format($bytes / 1024, 2) . ' kB'; | |
} | |
elseif ($bytes > 1) | |
{ | |
$bytes = $bytes . ' bytes'; | |
} | |
elseif ($bytes == 1) | |
{ | |
$bytes = $bytes . ' byte'; | |
} | |
else | |
{ | |
$bytes = '0 bytes'; | |
} | |
return $bytes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment