人类可读的文件大小

3zz*_*zzy 27 php filesize

function humanFileSize($size)
{
    if ($size >= 1073741824) {
      $fileSize = round($size / 1024 / 1024 / 1024,1) . 'GB';
    } elseif ($size >= 1048576) {
        $fileSize = round($size / 1024 / 1024,1) . 'MB';
    } elseif($size >= 1024) {
        $fileSize = round($size / 1024,1) . 'KB';
    } else {
        $fileSize = $size . ' bytes';
    }
    return $fileSize;
}
Run Code Online (Sandbox Code Playgroud)

...工作得很好,除了:我不能手动选择我需要显示的格式,比如我想以MB显示只有文件大小.目前,如果它在GB范围内,它只会以GB显示.

另外,如何将小数限制为2?

Nie*_*sol 48

尝试这样的事情:

function humanFileSize($size,$unit="") {
  if( (!$unit && $size >= 1<<30) || $unit == "GB")
    return number_format($size/(1<<30),2)."GB";
  if( (!$unit && $size >= 1<<20) || $unit == "MB")
    return number_format($size/(1<<20),2)."MB";
  if( (!$unit && $size >= 1<<10) || $unit == "KB")
    return number_format($size/(1<<10),2)."KB";
  return number_format($size)." bytes";
}
Run Code Online (Sandbox Code Playgroud)

  • 它应该是`$ unit =="GB"|| !$ unit && $ size> = 1 << 30`. (2认同)

Vai*_*das 36

Jeffrey Sambells有很好的例子:

function human_filesize($bytes, $dec = 2) 
{
    $size   = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
    $factor = floor((strlen($bytes) - 1) / 3);

    return sprintf("%.{$dec}f", $bytes / pow(1024, $factor)) . @$size[$factor];
}

print human_filesize(filesize('example.zip'));
Run Code Online (Sandbox Code Playgroud)

  • 很好,但我会添加 `if($factor==0) $dec=0;` (3认同)
  • 谷歌没有错.1MB = 1000000B.1MiB = 1048576B.他们只是遵循最新的IEC标准单位:https://en.wikipedia.org/wiki/Binary_prefix (2认同)
  • 对于返回行,您还可以使用: `return sprintf("%.{$dec}f %s", $bytes / (1024 ** $factor), $size[$factor]);` 它更干净一些。 (2认同)
  • @maki10 谢谢!我编写 `pow()` 已经有大约 10 年了,这是我第一次看到 `**` 运算符。太不可思议了,因为它从 5.6 版本开始就可用了(:我也同意 **sprintf 快捷方式**。您是否已将其作为编辑提交?如果没有,请这样做。 (2认同)

Roe*_*oey 11

我正在使用这种方法:

function byteConvert($bytes)
{
    if ($bytes == 0)
        return "0.00 B";

    $s = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
    $e = floor(log($bytes, 1024));

    return round($bytes/pow(1024, $e), 2).$s[$e];
}
Run Code Online (Sandbox Code Playgroud)

在o(1)中效果很好.


Krz*_*ski 6

为了扩展 Vaidas 的答案,您应该如何执行新的 IEC 标准:

function human_readable_bytes($bytes, $decimals = 2, $system = 'binary')
{
    $mod = ($system === 'binary') ? 1024 : 1000;

    $units = array(
        'binary' => array(
            'B',
            'KiB',
            'MiB',
            'GiB',
            'TiB',
            'PiB',
            'EiB',
            'ZiB',
            'YiB',
        ),
        'metric' => array(
            'B',
            'kB',
            'MB',
            'GB',
            'TB',
            'PB',
            'EB',
            'ZB',
            'YB',
        ),
    );

    $factor = floor((strlen($bytes) - 1) / 3);

    return sprintf("%.{$decimals}f%s", $bytes / pow($mod, $factor), $units[$system][$factor]);
}
Run Code Online (Sandbox Code Playgroud)

从技术上讲,根据存储设备等的规范,您应该使用公制系统作为默认值(这就是为什么 Google 转换器将 kB -> MB 显示为 mod 1000 而不是 1024)。


Car*_*ado 6

我使用(1024 = 1KB)并支持从 KB 到 YB 的一种非常短的 3 行方法如下:

<?php 

/**
 * Converts a long string of bytes into a readable format e.g KB, MB, GB, TB, YB
 * 
 * @param {Int} num The number of bytes.
 */
function readableBytes($bytes) {
    $i = floor(log($bytes) / log(1024));

    $sizes = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');

    return sprintf('%.02F', $bytes / pow(1024, $i)) * 1 . ' ' . $sizes[$i];
}

// "1000 B"
echo readableBytes(1000);

// "9.42 MB"
echo readableBytes(9874321);

// "9.31 GB"
// The number of bytes as a string is accepted as well
echo readableBytes("10000000000");

// "648.37 TB"
echo readableBytes(712893712304234);

// "5.52 PB"
echo readableBytes(6212893712323224);
Run Code Online (Sandbox Code Playgroud)

有关本文中这些方法的更多信息


100*_*bps 6

这是我的自定义函数,用于显示人类可读的文件大小:

function getHumanReadableSize($bytes) {
  if ($bytes > 0) {
    $base = floor(log($bytes) / log(1024));
    $units = array("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"); //units of measurement
    return number_format(($bytes / pow(1024, floor($base))), 3) . " $units[$base]";
  } else return "0 bytes";
}
Run Code Online (Sandbox Code Playgroud)