Created
December 18, 2014 12:24
-
-
Save juukie/723dd8ff9cfa706c1d6d to your computer and use it in GitHub Desktop.
format_filesize
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 | |
if ( ! function_exists('format_filesize')) | |
{ | |
/** | |
* Formats a filesize in a human readable way. | |
* | |
* @param int $size | |
* @param array $sizes | |
* @return string | |
*/ | |
function format_filesize($size, $sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']) | |
{ | |
// If you send 0 to the formula below, it will throw a division by zero error. | |
if ($size === 0) return '0 ' . $sizes[0]; | |
return (round($size / pow(1024, ($index = floor(log($size, 1024)))), 2).' '.$sizes[$index]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment