Skip to content

Instantly share code, notes, and snippets.

@ElectroDrome
Last active April 29, 2024 20:09
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 ElectroDrome/374b42422452561faacbde047300ff45 to your computer and use it in GitHub Desktop.
Save ElectroDrome/374b42422452561faacbde047300ff45 to your computer and use it in GitHub Desktop.
PHP: Format a number of bytes into a human readable format
// convert bytes format
function format_Bytes($size,$level=0,$precision=2,$base=1024)
{
if ($size === '0' || $size === null) {
return "0 B";
}
else
$unit = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB','YB');
$times = floor(log($size,$base));
return sprintf("%.".$precision."f",$size/pow($base,($times+$level)))." ".$unit[$times+$level];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment