Generate a human readable bytecount string.
@param  int  $bytes
@param  int $precision
@return string
423486
public static function readableSize($bytes, $precision = 1)
    {
        static $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
        if ($bytes === 0) {
            return '0 '.$units[0];
        }
        $exponent = floor(log($bytes, 1024));
        $value = $bytes / pow(1024, $exponent);

        return round($value, $precision).' '.$units[$exponent];
    }
933918
public static function byteStringToInt($bytes)
    {
        if (!self::isSupported()) {
            throw new \RuntimeException('This utility is only supported on 64 bit machines with PHP version > 5.5.');
        }
        if (self::isSystemLittleEndian()) {
            $bytes = strrev($bytes);
        }
        return unpack("q", $bytes)[1];
    }
958326
public static function formatByte($byte)
    {
        // ignore "Warning: Division by zero"
        if ($byte == 0) {
            return '0 B';
        }
        $s = array('B', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb');
        $e = floor(log($byte)/log(1024));
        return sprintf('%.2f '.$s[$e], ($byte/pow(1024, floor($e))));
    }
1228597
public function writeLine(string $bytes): int
    {
        $isAcceptable = $this->predicate;
        if ($isAcceptable($bytes)) {
            return $this->outputStream->writeLine($bytes);
        }

        return 0;
    }
1103681
public function memoryToHuman($bytes, $unit = "", $decimals = 2)
    {
        if($bytes <= 0)
            return '0.00 KB';

        $units = [
            'B' => 0,
            'KB' => 1,
            'MB' => 2,
            'GB' => 3,
            'TB' => 4,
            'PB' => 5,
            'EB' => 6,
            'ZB' => 7,
            'YB' => 8
        ];

        $value = 0;
        if ($bytes > 0)
        {
            // Generate automatic prefix by bytes
            // If wrong prefix given
            if ( ! array_key_exists($unit, $units))
            {
                $pow = floor(log($bytes) / log(1024));
                $unit = array_search($pow, $units);
            }

            // Calculate byte value by prefix
            $value = ($bytes / pow(1024, floor($units[$unit])));
        }

        // If decimals is not numeric or decimals is less than 0
        if ( ! is_numeric($decimals) || $decimals < 0)
            $decimals = 2;

        // Format output
        return sprintf('%.' . $decimals . 'f ' . $unit, $value);
    }
1076332
public static function GetSize($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;
	}
1059367
protected function formatBytes($bytes): string
    {
        $suffix = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

        $p = min((int)log($bytes, 1024), count($suffix) - 1);

        return sprintf('%1.2f%s', ($bytes / pow(1024, $p)), $suffix[$p]);
    }
619162
public static function humanize(
        $bytes,
        int $decimals = 2,
        array $sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
    ): string
    {
        $power = 0;
        $temp = $bytes;
        $count = (count($sizes) - 1);
        for ($i = 0; $i < $count; $i++) {
            if ($temp < 1024) {
                break;
            } else {
                $temp /= 1024;
                $power++;
            }
        }

        $humanize = sprintf("%.{$decimals}f", ($bytes / pow(1024, $power))) . $sizes[$power];

        return $humanize;
    }
785160
public function byteFormat( $bytes, $unit = "MB", $decimals = 2, $withUnit = false )
    {

        $units = array(
            'B' => 0,
            'KB' => 1,
            'MB' => 2,
            'GB' => 3,
            'TB' => 4,
            'PB' => 5,
            'EB' => 6,
            'ZB' => 7,
            'YB' => 8,
        );

        $value = 0;

        if ($bytes > 0) {
            /*
             * Generate automatic prefix by bytes
             * If wrong prefix given
             */
            if (!array_key_exists($unit, $units)) {
                $pow = floor(log($bytes)/log(1024));
                $unit = array_search($pow, $units);
            }

            /*
             * Calculate byte value by prefix
             */
            $value = ($bytes/pow(1024,floor($units[$unit])));
        } else {
            return 0;
        }

        /*
         * If decimals is not numeric or decimals is less than 0
         * then set default value
         */
        if (!is_numeric($decimals) || $decimals < 0) {
            $decimals = 2;
        }

        /*
         * Format output
         */
        if( $withUnit ) {
            return sprintf('%.' . $decimals . 'f '.$unit, $value);
        } else {
            return sprintf('%.' . $decimals . 'f', $value);
        }
    }
1229071
public static function formatMemory($bytes, $precision = 3)
    {
        $absBytes = abs($bytes);
        $sign = ($bytes == $absBytes) ? 1 : -1;
        $suffix = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
        $total = count($suffix);

        for ($i = 0; $absBytes > 1024 && $i < $total; $i++) {
            $absBytes /= 1024;
        }

        return self::toPrecision($sign * $absBytes, $precision) . $suffix[$i];
    }
1333533
public function write(string $bytes): int
    {
        return $this->outputStream->write(iconv('UTF-8', $this->charset, $bytes));
    }
1040607
public function bytes($bytes, $precision = 2)
    {
        $bytes = \round($bytes);
        $units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB'];
        foreach ($units as $unit) {
            if (\abs($bytes) < 1024 || $unit === \end($units)) {
                break;
            }
            $bytes = $bytes / 1024;
        }

        return \round($bytes, $precision) . ' ' . $unit;
    }
460733
def fmt_bytes(bytes, precision=2):
    
    UNITS = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB']

    if bytes == 0:
        return '0 bytes'

    log = math.floor(math.log(bytes, 1000))

    return "%.*f %s" % (precision,
                       bytes / math.pow(1000, log),
                       UNITS[int(log)])
451051
static function bytes($bytes = Constants::BYTES)
    {
        # Test the length for validity.
        Helpers::rangeCheck($bytes, Constants::BYTES_MAX, Constants::BYTES_MIN, 'Entropy', 'bytes');

        return \Sodium\randombytes_buf($bytes);
    }
487028
public function write(string $bytes): int
    {
        if ($this->isFileCreationDelayed()) {
            $this->setHandle($this->openFile($this->file, $this->mode));
        }

        return parent::write($bytes);
    }
1152315
protected function doCommit($bytes)
    {
        if (isset($this->in)) {
            $bytesToWrite = strlen($bytes);
            $totalBytesWritten = 0;

            while ($totalBytesWritten < $bytesToWrite) {
                $bytesWritten = fwrite($this->in, substr($bytes, $totalBytesWritten));
                if (false === $bytesWritten || 0 === $bytesWritten) {
                    break;
                }

                $totalBytesWritten += $bytesWritten;
            }

            if ($totalBytesWritten > 0) {
                return ++$this->sequence;
            }
        }
    }
874278
public static function bytesToString(int $bytes, int $round = 0): string
    {
        return self::getMeasure($bytes, 0, $round);
    }