Skip to content

Instantly share code, notes, and snippets.

@jk2K
Created March 19, 2014 07:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jk2K/9636901 to your computer and use it in GitHub Desktop.
Save jk2K/9636901 to your computer and use it in GitHub Desktop.
格式化文件大小
function size_format( $bytes, $decimals = 0 ) {
$quant = array(
// ========================= Origin ====
'TB' => 1099511627776, // pow( 1024, 4)
'GB' => 1073741824, // pow( 1024, 3)
'MB' => 1048576, // pow( 1024, 2)
'kB' => 1024, // pow( 1024, 1)
'B ' => 1, // pow( 1024, 0)
);
foreach ( $quant as $unit => $mag )
if ( doubleval($bytes) >= $mag )
return number_format( $bytes / $mag, $decimals ) . ' ' . $unit;
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment