Skip to content

Instantly share code, notes, and snippets.

@rotexdegba
Created October 13, 2016 04:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rotexdegba/f53462a49269d36764d85dad02026dc4 to your computer and use it in GitHub Desktop.
Save rotexdegba/f53462a49269d36764d85dad02026dc4 to your computer and use it in GitHub Desktop.
Format Bytes to Readable Unit
<?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