Search code examples
phplinuxfilesize

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


I'm using this function to get the file size

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];}

but I'm getting different result when using Linux command ls -alh

php return 2.31mb

Linux return 2.4mb


Solution

  • What Linux is showing you are SI megabytes (1000 based units), not IEC megabytes (1024 based units)

    2.31*1024*1024/1000/1000 =  2.422211
    

    in accordance with their policy of representing numbers. For network bandwidth and disk sizes they use SI metric, i.e. 1 MB = 1,000 kB = 1,000,000 bytes.

    For memory sizes they use IEC metric, i.e. 1 MiB = 1,024 KiB = 1,048,576 bytes.