08c02-3.php
来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 27 行
PHP
27 行
<?php// A function to format the bytes of a file, as a natural looking string.// This version handles it via 1024 based logic:function format_bytes($bytes) { // Define an array of the different display forms: // These are the SI approved binary variations: $display = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'); // Now, constantly divide the value by 1024 until it is less than 1024 $level = 0; while ($bytes > 1024) { $bytes /= 1024; $level++; } // Now we have our final value, format it to just 1 decimal place // and append on to the the appropriate level moniker. return round($bytes, 1) . ' ' . $display[$level];}// Echo out a few examples to test the function:echo '<p>4200 bytes = ', format_bytes(4200), "<p>\n"; // 4.1 KiBecho '<p>420000 bytes = ', format_bytes(420000), "<p>\n"; // 410.2 KiBecho '<p>42000000 bytes = ', format_bytes(42000000), "<p>\n"; // 40.1 MiB?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?