Created
March 28, 2014 20:30
-
-
Save mohitmamoria/9842328 to your computer and use it in GitHub Desktop.
Convert filesize from bytes to human readable format in PHP
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
/** | |
* 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