Swat/SwatString.php
public static function byteFormat(
        $value,
        $magnitude = -1,
        $iec_units = false,
        $significant_digits = 3
    ) {
        if ($iec_units) {
            $units = array(
                60 => 'EiB',
                50 => 'PiB',
                40 => 'TiB',
                30 => 'GiB',
                20 => 'MiB',
                10 => 'KiB',
                0 => 'bytes'
            );
        } else {
            $units = array(
                60 => 'EB',
                50 => 'PB',
                40 => 'TB',
                30 => 'GB',
                20 => 'MB',
                10 => 'KB',
                0 => 'bytes'
            );
        }

        $unit_magnitude = null;

        if ($magnitude >= 0) {
            $magnitude = intval(round($magnitude / 10) * 10);
            if (array_key_exists($magnitude, $units)) {
                $unit_magnitude = $magnitude;
            } else {
                $unit_magnitude = reset($units); // default magnitude
            }
        } else {
            if ($value == 0) {
                $unit_magnitude = 0;
            } else {
                $log = floor(log10($value) / log10(2)); // get log2()

                $unit_magnitude = reset($units); // default magnitude
                foreach ($units as $magnitude => $title) {
                    if ($log >= $magnitude) {
                        $unit_magnitude = $magnitude;
                        break;
                    }
                }
            }
        }

        $value = $value / pow(2, $unit_magnitude);

        if ($unit_magnitude == 0) {
            // 'bytes' are always formatted as integers
            $formatted_value = self::numberFormat($value, 0);
        } else {
            if ($significant_digits !== null) {
                // round to number of significant digits
                $integer_digits = floor(log10($value)) + 1;

                $fractional_digits = max(
                    $significant_digits - $integer_digits,
                    0
                );

                $formatted_value = self::numberFormat(
                    $value,
                    $fractional_digits
                );
            } else {
                // just round to one fractional digit
                $formatted_value = self::numberFormat($value, 1);
            }
        }

        return $formatted_value . ' ' . $units[$unit_magnitude];
    }
public static function byteFormat(
        $value,
        $magnitude = -1,
        $iec_units = false,
        $significant_digits = 3
    ) {
        if ($iec_units) {
            $units = array(
                60 => 'EiB',
                50 => 'PiB',
                40 => 'TiB',
                30 => 'GiB',
                20 => 'MiB',
                10 => 'KiB',
                0 => 'bytes'
            );
        } else {
            $units = array(
                60 => 'EB',
                50 => 'PB',
                40 => 'TB',
                30 => 'GB',
                20 => 'MB',
                10 => 'KB',
                0 => 'bytes'
            );
        }

        $unit_magnitude = null;

        if ($magnitude >= 0) {
            $magnitude = intval(round($magnitude / 10) * 10);
            if (array_key_exists($magnitude, $units)) {
                $unit_magnitude = $magnitude;
            } else {
                $unit_magnitude = reset($units); // default magnitude
            }
        } else {
            if ($value == 0) {
                $unit_magnitude = 0;
            } else {
                $log = floor(log10($value) / log10(2)); // get log2()

                $unit_magnitude = reset($units); // default magnitude
                foreach ($units as $magnitude => $title) {
                    if ($log >= $magnitude) {
                        $unit_magnitude = $magnitude;
                        break;
                    }
                }
            }
        }

        $value = $value / pow(2, $unit_magnitude);

        if ($unit_magnitude == 0) {
            // 'bytes' are always formatted as integers
            $formatted_value = self::numberFormat($value, 0);
        } else {
            if ($significant_digits !== null) {
                // round to number of significant digits
                $integer_digits = floor(log10($value)) + 1;

                $fractional_digits = max(
                    $significant_digits - $integer_digits,
                    0
                );

                $formatted_value = self::numberFormat(
                    $value,
                    $fractional_digits
                );
            } else {
                // just round to one fractional digit
                $formatted_value = self::numberFormat($value, 1);
            }
        }

        return $formatted_value . ' ' . $units[$unit_magnitude];
    }
public static function byteFormat ( $ value , $ magnitude = - 1 , $ iec_units = false , $ significant_digits = 3 ) { if ( $ iec_units ) { $ units = array ( 60 => 'EiB' , 50 => 'PiB' , 40 => 'TiB' , 30 => 'GiB' , 20 => 'MiB' , 10 => 'KiB' , 0 => 'bytes' ) ; } else { $ units = array ( 60 => 'EB' , 50 => 'PB' , 40 => 'TB' , 30 => 'GB' , 20 => 'MB' , 10 => 'KB' , 0 => 'bytes' ) ; } $ unit_magnitude = null ; if ( $ magnitude >= 0 ) { $ magnitude = intval ( round ( $ magnitude / 10 ) * 10 ) ; if ( array_key_exists ( $ magnitude , $ units ) ) { $ unit_magnitude = $ magnitude ; } else { $ unit_magnitude = reset ( $ units ) ; // default magnitude } } else { if ( $ value == 0 ) { $ unit_magnitude = 0 ; } else { $ log = floor ( log10 ( $ value ) / log10 ( 2 ) ) ; // get log2() $ unit_magnitude = reset ( $ units ) ; // default magnitude foreach ( $ units as $ magnitude => $ title ) { if ( $ log >= $ magnitude ) { $ unit_magnitude = $ magnitude ; break ; } } } } $ value = $ value / pow ( 2 , $ unit_magnitude ) ; if ( $ unit_magnitude == 0 ) { // 'bytes' are always formatted as integers $ formatted_value = self :: numberFormat ( $ value , 0 ) ; } else { if ( $ significant_digits !== null ) { // round to number of significant digits $ integer_digits = floor ( log10 ( $ value ) ) + 1 ; $ fractional_digits = max ( $ significant_digits - $ integer_digits , 0 ) ; $ formatted_value = self :: numberFormat ( $ value , $ fractional_digits ) ; } else { // just round to one fractional digit $ formatted_value = self :: numberFormat ( $ value , 1 ) ; } } return $ formatted_value . ' ' . $ units [ $ unit_magnitude ] ; }
Format bytes in human readible units

By default, bytes are formatted using canonical, ambiguous, base-10
prefixed units. Bytes may optionally be formatted using unambiguous IEC
standard binary prefixes. See the National Institute of Standards and
Technology's page on binary unit prefixes at
{@link http://physics.nist.gov/cuu/Units/binary.html} for details.

@param integer $value the value in bytes to format.
@param integer $magnitude optional. The power of 2 to use as the unit
base. This value will be rounded to the
nearest ten if specified. If less than zero
or not specified, the highest power less
than <code>$value</code> will be used.
@param boolean $iec_units optional. Whether or not to use IEC binary
multiple prefixed units (Mebibyte). Defaults
to using canonical units.
@param integer $significant_digits optional. The number of significant
digits in the formatted result. If
null, the value will be rounded and
formatted one fractional digit.
Otherwise, the value is rounded to
the specified the number of digits.
By default, this is three. If there
are more integer digits than the
specified number of significant
digits, the value is rounded to the
nearest integer.

@return string the byte value formated according to IEC units.
Format bytes in human readible units