Skip to content

Instantly share code, notes, and snippets.

@Daniel-Walsh
Last active October 1, 2021 02:28
Show Gist options
  • Save Daniel-Walsh/2036e7b5496835a20af8d01bc7b5c616 to your computer and use it in GitHub Desktop.
Save Daniel-Walsh/2036e7b5496835a20af8d01bc7b5c616 to your computer and use it in GitHub Desktop.
Returns a human readable filesize, given a filesize in bytes #php
<?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