human readable file size

前端 未结 11 2216
梦毁少年i
梦毁少年i 2020-12-25 10:48
function humanFileSize($size)
{
    if ($size >= 1073741824) {
      $fileSize = round($size / 1024 / 1024 / 1024,1) . \'GB\';
    } elseif ($size >= 1048576)          


        
相关标签:
11条回答
  • 2020-12-25 11:28

    This is how I use, it's clean and simple. Also can be used like this:

    public function getHumanReadableFilesize(int $bytes, int $dec = 2): string
    {
        $size   = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
        $factor = floor((strlen($bytes) - 1) / 3);
    
        return sprintf("%.{$dec}f %s", ($bytes / (1024 ** $factor)), $size[$factor]);
    }
    

    Thanks @Márton Tamás for suggestion to add like comment.

    0 讨论(0)
  • 2020-12-25 11:29

    Try something like this:

    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";
    }
    
    0 讨论(0)
  • 2020-12-25 11:32

    To expand on Vaidas' answer, here's how you should do it to account for the new IEC standards:

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

    Technically, according to the specifications for storage devices and such you should use the metric system as default (that's why Google converter shows kB -> MB as mod 1000 instead of 1024).

    0 讨论(0)
  • 2020-12-25 11:35

    I'm using this method:

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

    works great in o(1).

    0 讨论(0)
  • 2020-12-25 11:35

    I wanted a function that returned filesizes like Windows does, and surprisingly I could find none at all. Even worse, some here and elsewhere are broken in that they assume 1KB = 1000B.

    So I coded one! Plus two helper functions. Here they are:

    // Returns a size in a human-readable form from a byte count.
    function humanSize($bytes)
    {
        if ($bytes < 1024) return "$bytes Bytes";
    
        $units = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
    
        foreach ($units as $i => $unit)
        {
            // The reason for this threshold is to avoid e.g., "1000 KB",
            // instead jumping from e.g., "999 KB" to "0.97 MB".
            $multiplier = pow(1024, $i + 1);
            $threshold = $multiplier * 1000;
    
            if ($bytes < $threshold)
            {
                $size = formatToMinimumDigits($bytes / $multiplier, false);
                return "$size $unit";
            }
        }
    }
    
    // Efficiently calculates how many digits the integer portion of a number has.
    function digits($number)
    {
        // Yes, I could convert to string and count the characters,
        // but this is faster and cooler.
        $log = log10($number);
        if ($log < 0) return 1;
        return floor($log) + 1;
    }
    
    // Formats a number to a minimum amount of digits.
    // In other words, makes sure that a number has at least $digits on it, even if
    // that means introducing redundant decimal zeroes at the end, or rounding the
    // ones present exceeding the $digits count when combined with the integers.
    // For example:
    //     formatToMinimumDigits(10)           // 10.0
    //     formatToMinimumDigits(1.1)          // 1.10
    //     formatToMinimumDigits(12.34)        // 12.3
    //     formatToMinimumDigits(1.234)        // 1.23
    //     formatToMinimumDigits(1.203)        // 1.20
    //     formatToMinimumDigits(123.4)        // 123
    //     formatToMinimumDigits(100)          // 100
    //     formatToMinimumDigits(1000)         // 1000
    //     formatToMinimumDigits(1)            // 1.00
    //     formatToMinimumDigits(1.002)        // 1.00
    //     formatToMinimumDigits(1.005)        // 1.01
    //     formatToMinimumDigits(1.005, false) // 1.00
    // This is primarily useful for generating human-friendly numbers.
    function formatToMinimumDigits($value, $round = true, $digits = 3)
    {
        $integers = floor($value);
    
        $decimalsNeeded = $digits - digits($integers);
    
        if ($decimalsNeeded < 1)
        {
            return $integers;
        }
        else
        {
            if ($round)
            {
                // This relies on implicit type casting of float to string.
                $parts = explode('.', round($value, $decimalsNeeded));
                // We re-declare the integers because they may change
                // after we round the number.
                $integers = $parts[0];
            }
            else
            {
                // Again, implicit type cast to string.
                $parts = explode('.', $value);
            }
    
            // And because of the implicit type cast, we must guard against
            // 1.00 becoming 1, thus not exploding the second half of it.
            $decimals = isset($parts[1]) ? $parts[1] : '0';
            $joined = "$integers.$decimals".str_repeat('0', $digits);
            return substr($joined, 0, $digits + 1);
        }
    }
    

    Usage is as simple as humanSize(123456789).

    0 讨论(0)
  • 2020-12-25 11:36
    function bytesToHuman($bytes)
    {
        $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
        for ($i = 0; $bytes > 1024; $i++) $bytes /= 1024;
        return round($bytes, 2) . ' ' . $units[$i];
    }
    

    Credit: https://laracasts.com/discuss/channels/laravel/human-readable-file-size-and-time?page=1#reply=115796

    0 讨论(0)
提交回复
热议问题