Created
March 19, 2014 07:18
-
-
Save jk2K/9636901 to your computer and use it in GitHub Desktop.
格式化文件大小
This file contains hidden or 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
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