龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php转换文件大小为易于阅读的格式

时间:2014-06-23 03:02来源:网络整理 作者:网络 点击:
分享到:
php转换文件大小为易于阅读的格式 [代码片段(32行)]
// A much better and accurate version can be found
// in Aidan's PHP Repository: 
// http://aidanlister.com/repos/v/function.size_readable.php

/**
 * Returns a human readable filesize
 *
 * @author      wesman20 (php.net)
 * @author      Jonas John
 * @version     0.3
 * @link        http://www.jonasjohn.de/snippets/php/readable-filesize.htm
 */
function HumanReadableFilesize($size) {

    // Adapted from: http://www.php.net/manual/en/function.filesize.php

    $mod = 1024;

    $units = explode(' ','B KB MB GB TB PB');
    for ($i = 0; $size > $mod; $i++) {
        $size /= $mod;
    }

    return round($size, 2) . ' ' . $units[$i];
}

// Example

print HumanReadableFilesize(filesize('test_2mb.zip'));

// Returns: 
// --> 2 MB
精彩图集

赞助商链接