PHP filesize MB/KB转换

Ale*_*lex 121 php file filesize

如何filesize()使用MegaBytes,KiloBytes等将PHP 函数的输出转换为漂亮的格式?

喜欢:

  • 如果大小小于1 MB,则以KB为单位显示大小
  • 如果它在1 MB之间 - 1 GB以MB为单位显示
  • 如果它更大 - 以GB为单位

Adn*_*nan 258

这是一个示例:

<?php
// Snippet from PHP Share: http://www.phpshare.org

    function formatSizeUnits($bytes)
    {
        if ($bytes >= 1073741824)
        {
            $bytes = number_format($bytes / 1073741824, 2) . ' GB';
        }
        elseif ($bytes >= 1048576)
        {
            $bytes = number_format($bytes / 1048576, 2) . ' MB';
        }
        elseif ($bytes >= 1024)
        {
            $bytes = number_format($bytes / 1024, 2) . ' KB';
        }
        elseif ($bytes > 1)
        {
            $bytes = $bytes . ' bytes';
        }
        elseif ($bytes == 1)
        {
            $bytes = $bytes . ' byte';
        }
        else
        {
            $bytes = '0 bytes';
        }

        return $bytes;
}
?>
Run Code Online (Sandbox Code Playgroud)

  • 这个答案比下面的其他答案更有效.它避免使用日志函数或连续的除法语句来识别单位. (4认同)

PiT*_*ber 61

更好的是我从我发现的插件创建的这个版本:

function filesize_formatted($path)
{
    $size = filesize($path);
    $units = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
    $power = $size > 0 ? floor(log($size, 1024)) : 0;
    return number_format($size / pow(1024, $power), 2, '.', ',') . ' ' . $units[$power];
}
Run Code Online (Sandbox Code Playgroud)

来自filesize()doc的注释

由于PHP的整数类型已签名且许多平台使用32位整数,因此某些文件系统函数可能会对大于2GB的文件返回意外结果

  • @AlixAxel它更好,因为它的大小只有一半,而且仍然易于阅读.我还打赌它更快.但是,嘿,这不是个人的.我自己没想到.你的版本也很酷!赞成它;) (6认同)

Ali*_*xel 42

更清洁的方法:

function Size($path)
{
    $bytes = sprintf('%u', filesize($path));

    if ($bytes > 0)
    {
        $unit = intval(log($bytes, 1024));
        $units = array('B', 'KB', 'MB', 'GB');

        if (array_key_exists($unit, $units) === true)
        {
            return sprintf('%d %s', $bytes / pow(1024, $unit), $units[$unit]);
        }
    }

    return $bytes;
}
Run Code Online (Sandbox Code Playgroud)

  • 我不敢相信你们在这个更清洁,更优雅的答案中正在考虑对数方程式的"成本".你是如何对这种差异进行基准的?并且,如果日志成本太高,为什么不在应用单位附录之前以比较=> 1024开始函数? (4认同)

Tef*_*ffi 14

我认为这是一种更好的方法.简单直接.

public function sizeFilter( $bytes )
{
    $label = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB' );
    for( $i = 0; $bytes >= 1024 && $i < ( count( $label ) -1 ); $bytes /= 1024, $i++ );
    return( round( $bytes, 2 ) . " " . $label[$i] );
}
Run Code Online (Sandbox Code Playgroud)


miy*_*uru 7

该问题的所有答案都使用 1 KB = 1024 字节,这是错误的!(1 kibibyte = 1024 字节

由于问题要求转换文件大小,因此应使用1 KB = 1000 字节(请参阅https://wiki.ubuntu.com/UnitsPolicy

function format_bytes($bytes, $precision = 2) {
    $units = array('B', 'KB', 'MB', 'GB');

    $bytes = max($bytes, 0);
    $pow = floor(($bytes ? log($bytes) : 0) / log(1000));
    $pow = min($pow, count($units) - 1);

    $bytes /= pow(1000, $pow);

    return round($bytes, $precision) . ' ' . $units[$pow];
}
Run Code Online (Sandbox Code Playgroud)


Jen*_*och 6

这是基于@adnan的绝佳答案.

变化:

  • 添加了内部filesize()调用
  • 回归早期风格
  • 在1个字节上保存一个连接

你仍然可以将filesize()调用拉出函数,以获得纯字节格式化函数.但这适用于文件.


/**
 * Formats filesize in human readable way.
 *
 * @param file $file
 * @return string Formatted Filesize, e.g. "113.24 MB".
 */
function filesize_formatted($file)
{
    $bytes = filesize($file);

    if ($bytes >= 1073741824) {
        return number_format($bytes / 1073741824, 2) . ' GB';
    } elseif ($bytes >= 1048576) {
        return number_format($bytes / 1048576, 2) . ' MB';
    } elseif ($bytes >= 1024) {
        return number_format($bytes / 1024, 2) . ' KB';
    } elseif ($bytes > 1) {
        return $bytes . ' bytes';
    } elseif ($bytes == 1) {
        return '1 byte';
    } else {
        return '0 bytes';
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

这将是一个更清洁的实现:

function size2Byte($size) {
    $units = array('KB', 'MB', 'GB', 'TB');
    $currUnit = '';
    while (count($units) > 0  &&  $size > 1024) {
        $currUnit = array_shift($units);
        $size /= 1024;
    }
    return ($size | 0) . $currUnit;
}
Run Code Online (Sandbox Code Playgroud)