Convert bytes into a human-readable format.

For example: $bytes = 1024 would return 1KB, $bytes = 1048576 would return 1MB, etc.

@param int $bytes The number of bytes to convert.
@return string The human readable representation of the bytes.
1381
public static function make_human_readable_filesize($bytes) {
		$units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
		$num_units = count($units);
		$size = $bytes;
		foreach ($units as $i => $unit) {
			if ($size < 1024 || (($i + 1) === $num_units)) {
				return round($size, 2).$unit;
			}
			$size = $size / 1024;
		}
		return $size;
	}
1140480
public function decode($bytes){
        $bytes->setPosition(0);
        $blocks = $this->decodeToBlocks($bytes);

        return $this->expandBlocks($blocks);
    }
1200140
static public function bytesToSize($bytes, $precision = 2)
    {
        $kilobyte = 1024;
        $megabyte = $kilobyte * 1024;
        $gigabyte = $megabyte * 1024;
        $terabyte = $gigabyte * 1024;

        if (($bytes >= 0) && ($bytes < $kilobyte)) {
            return $bytes . ' B';

        } elseif (($bytes >= $kilobyte) && ($bytes < $megabyte)) {
            return round($bytes / $kilobyte, $precision) . ' KB';

        } elseif (($bytes >= $megabyte) && ($bytes < $gigabyte)) {
            return round($bytes / $megabyte, $precision) . ' MB';

        } elseif (($bytes >= $gigabyte) && ($bytes < $terabyte)) {
            return round($bytes / $gigabyte, $precision) . ' GB';

        } elseif ($bytes >= $terabyte) {
            return round($bytes / $terabyte, $precision) . ' TB';
        } else {
            return $bytes . ' B';
        }
    }
596066
public function EntryRead($Bytes = -1)
    {
        $Actual = $this->EntryActualSize();
        if ($Bytes < 0 || $Bytes > $Actual) {
            $Bytes = $Actual;
        }
        return substr($this->Data, $this->Offset + 512, $Bytes);
    }
1063902
def convert_bytes(bytes_, K=1 << 10, M=1 << 20, G=1 << 30, T=1 << 40):
    """"""
    fn = float(bytes_)
    if bytes_ >= T:
        return '%.1fT' % (fn / T)
    elif bytes_ >= G:
        return '%.1fG' % (fn / G)
    elif bytes_ >= M:
        return '%.1fM' % (fn / M)
    elif bytes_ >= K:
        return '%.1fK' % (fn / K)
    else:
        return '%d' % bytes_
352376
public static function fromBytes(array $bytes)
    {
        $result = null;
        if (count($bytes) === 4) {
            $chunks = array_map(
                function ($byte) {
                    return (is_int($byte) && $byte >= 0 && $byte <= 255) ? (string) $byte : false;
                },
                $bytes
            );
            if (in_array(false, $chunks, true) === false) {
                $result = new static(implode('.', $chunks));
            }
        }

        return $result;
    }
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;
    }
879847
public static function size_format($bytes, $decimals = 0)
    {
        $bytes = floatval($bytes);

        if ($bytes < 1024) {
            return $bytes . ' B';
        } elseif ($bytes < pow(1024, 2)) {
            return number_format($bytes / 1024, $decimals, '.', '') . ' KiB';
        } elseif ($bytes < pow(1024, 3)) {
            return number_format($bytes / pow(1024, 2), $decimals, '.', '') . ' MiB';
        } elseif ($bytes < pow(1024, 4)) {
            return number_format($bytes / pow(1024, 3), $decimals, '.', '') . ' GiB';
        } elseif ($bytes < pow(1024, 5)) {
            return number_format($bytes / pow(1024, 4), $decimals, '.', '') . ' TiB';
        } elseif ($bytes < pow(1024, 6)) {
            return number_format($bytes / pow(1024, 5), $decimals, '.', '') . ' PiB';
        } else {
            return number_format($bytes / pow(1024, 5), $decimals, '.', '') . ' PiB';
        }
    }
1099994
private function fileSizeConvert(int $bytes)
    {
        $bytes = floatval($bytes);
        $result = "0 B";
        $arBytes = array(
            0 => array(
                "UNIT" => "TB",
                "VALUE" => pow(1024, 4)
            ),
            1 => array(
                "UNIT" => "GB",
                "VALUE" => pow(1024, 3)
            ),
            2 => array(
                "UNIT" => "MB",
                "VALUE" => pow(1024, 2)
            ),
            3 => array(
                "UNIT" => "KB",
                "VALUE" => 1024
            ),
            4 => array(
                "UNIT" => "B",
                "VALUE" => 1
            ),
        );

        foreach ($arBytes as $arItem) {
            if ($bytes >= $arItem["VALUE"]) {
                $result = $bytes / $arItem["VALUE"];
                $result = str_replace(".", ",", strval(round($result, 2)))." ".$arItem["UNIT"];
                break;
            }
        }
        return $result;
    }
546225
public function read($bytes)
    {
        $content = $this->file->data();

        $return = substr($content, $this->position(), $bytes);

        $newPosition = $this->position()+$bytes;

        $newPosition = $newPosition > strlen($content) ? strlen($content) : $newPosition;

        $this->position($newPosition);
        $this->file->setAccessTime(time());

        return $return;
    }
516719
protected static function randomTwister($bytes)
    {
        $rand = "";
        for ($a = 0; $a < $bytes; $a++) {
            $rand .= chr(mt_rand(0, 255));
        }

        return $rand;
    }
352473
public function read($bytes)
    {
        $read = 0;
        $parts = array();

        while ($read < $bytes) {
            $data = fread($this->socket, $bytes - $read);

            if ($data === false) {
                // server must have dropped the connection
                if (feof($this->socket)) {
                    $this->close();
                }

                return false;
            }

            $read += strlen($data);
            $parts[] = $data;
        }

        return implode($parts);
    }
1333533
public function write(string $bytes): int
    {
        return $this->outputStream->write(iconv('UTF-8', $this->charset, $bytes));
    }
1082699
public static String getSizeFromBytes(long bytes) {
    double ret = bytes;
    if (ret <= 1024 * 5) {
      return String.format(Locale.ENGLISH, "%dB", bytes);
    }
    ret /= 1024;
    if (ret <= 1024 * 5) {
      return String.format(Locale.ENGLISH, "%.2fKB", ret);
    }
    ret /= 1024;
    if (ret <= 1024 * 5) {
      return String.format(Locale.ENGLISH, "%.2fMB", ret);
    }
    ret /= 1024;
    if (ret <= 1024 * 5) {
      return String.format(Locale.ENGLISH, "%.2fGB", ret);
    }
    ret /= 1024;
    if (ret <= 1024 * 5) {
      return String.format(Locale.ENGLISH, "%.2fTB", ret);
    }
    ret /= 1024;
    return String.format(Locale.ENGLISH, "%.2fPB", ret);
  }
77455
private function convertWithBytes($size)
    {
        $precision = 2;
        $base      = log($size, 1024);
        $suffixes  = array('B', 'KB', 'MB', 'GB', 'TB');
        $bytes     = round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];

        return $bytes;
    }
380353
def _num_bytes_to_human_readable(num_bytes):
  """"""
  if num_bytes < (2 ** 10):
    return "%d B" % num_bytes
  elif num_bytes < (2 ** 20):
    return "%.3f KB" % (float(num_bytes) / (2 ** 10))
  elif num_bytes < (2 ** 30):
    return "%.3f MB" % (float(num_bytes) / (2 ** 20))
  else:
    return "%.3f GB" % (float(num_bytes) / (2 ** 30))
1064223
public static function bytes_pretty_print($bytes)
    {
        if ($bytes < 1024) {
            return $bytes . " B";
        }

        $kbytes = $bytes / 1024;

        if ($kbytes < 1024) {
            return round($kbytes, 3) . " KB";
        }

        $mbytes = $kbytes / 1024;

        if ($mbytes < 1024) {
            return round($mbytes, 3) . " MB";
        }

        $tbytes = $mbytes / 1024;

        if ($tbytes < 1024) {
            return round($tbytes, 3) . " TB";
        }

        $gbytes = $tbytes / 1024;

        return round($gbytes, 3) . " GB";
    }