Last active
October 1, 2021 02:28
-
-
Save Daniel-Walsh/2036e7b5496835a20af8d01bc7b5c616 to your computer and use it in GitHub Desktop.
Returns a human readable filesize, given a filesize in bytes #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
<?php | |
/** | |
* Returns a human readable filesize, given a filesize in bytes. | |
* | |
* @param integer $bytes The filesize to convert in bytes. | |
* @param integer $decimals The number of decimal places to return. Default is 2. | |
* @return string | |
*/ | |
function get_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