Home > Backend Development > PHP Tutorial > PHP使用适合阅读的格式显示文件大小的方法_php技巧

PHP使用适合阅读的格式显示文件大小的方法_php技巧

WBOY
Release: 2016-05-16 20:22:04
Original
1040 people have browsed it

本文实例讲述了PHP使用适合阅读的格式显示文件大小的方法。分享给大家供大家参考。具体分析如下:

文件大小显示,例如 1.7K , 2.9M
代码如下:

复制代码 代码如下:
// 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];
}

希望本文所述对大家的php程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template