Skip to content

Instantly share code, notes, and snippets.

@mohitmamoria
Created March 28, 2014 20:30
Show Gist options
  • Save mohitmamoria/9842328 to your computer and use it in GitHub Desktop.
Save mohitmamoria/9842328 to your computer and use it in GitHub Desktop.
Convert filesize from bytes to human readable format in PHP
/**
* Converts filesize from bytes to human readable format.
*
* @param number $bytes
* @param number $decimals
* @return string
*/
function human_filesize($bytes, $decimals = 2)
{
$size = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . ' ' . $size[$factor];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment