使用 filesize 函数和 Linux ls -alh 时得到不同的文件大小

getting different file size when using filesize function and Linux ls -alh

我正在使用这个函数来获取文件大小

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

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

return round($bytes/pow(1024, $e), 2).$s[$e];}

但是我在使用 Linux 命令 ls -alh

时得到不同的结果

php return 2.31mb

Linux return 2.4mb

Linux 显示的是 SI 兆字节(基于 1000 的单位),而不是 IEC 兆字节(基于 1024 的单位)

2.31*1024*1024/1000/1000 =  2.422211

根据their policy of representing numbers。对于网络带宽和磁盘大小,他们使用 SI 度量,即 1 MB = 1,000 kB = 1,000,000 字节。

对于内存大小,他们使用 IEC 度量,即 1 MiB = 1,024 KiB = 1,048,576 字节。