size_format

The timeline below displays how wordpress function size_format has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Converts a number of bytes to the largest unit the bytes will fit into.
 *
 * It is easier to read 1 KB than 1024 bytes and 1 MB than 1048576 bytes. Converts
 * number of bytes to human readable number by taking the number of that unit
 * that the bytes will go into it. Supports YB value.
 *
 * Please note that integers in PHP are limited to 32 bits, unless they are on
 * 64 bit architecture, then they have 64 bit size. If you need to place the
 * larger size then what PHP integer type will hold, then use a string. It will
 * be converted to a double, which should always have 64 bit length.
 *
 * Technically the correct unit names for powers of 1024 are KiB, MiB etc.
 *
 * @since 2.3.0
 * @since 6.0.0 Support for PB, EB, ZB, and YB was added.
 *
 * @param int|string $bytes    Number of bytes. Note max integer size for integers.
 * @param int        $decimals Optional. Precision of number of decimal places. Default 0.
 * @return string|false Number string on success, false on failure.
 */
function size_format($bytes, $decimals = 0)
{
    $quant = array(
        /* translators: Unit symbol for yottabyte. */
        _x('YB', 'unit symbol') => YB_IN_BYTES,
        /* translators: Unit symbol for zettabyte. */
        _x('ZB', 'unit symbol') => ZB_IN_BYTES,
        /* translators: Unit symbol for exabyte. */
        _x('EB', 'unit symbol') => EB_IN_BYTES,
        /* translators: Unit symbol for petabyte. */
        _x('PB', 'unit symbol') => PB_IN_BYTES,
        /* translators: Unit symbol for terabyte. */
        _x('TB', 'unit symbol') => TB_IN_BYTES,
        /* translators: Unit symbol for gigabyte. */
        _x('GB', 'unit symbol') => GB_IN_BYTES,
        /* translators: Unit symbol for megabyte. */
        _x('MB', 'unit symbol') => MB_IN_BYTES,
        /* translators: Unit symbol for kilobyte. */
        _x('KB', 'unit symbol') => KB_IN_BYTES,
        /* translators: Unit symbol for byte. */
        _x('B', 'unit symbol') => 1,
    );
    if (0 === $bytes) {
        /* translators: Unit symbol for byte. */
        return number_format_i18n(0, $decimals) . ' ' . _x('B', 'unit symbol');
    }
    foreach ($quant as $unit => $mag) {
        if ((float) $bytes >= $mag) {
            return number_format_i18n($bytes / $mag, $decimals) . ' ' . $unit;
        }
    }
    return false;
}

WordPress Version: 5.6

/**
 * Convert number of bytes largest unit bytes will fit into.
 *
 * It is easier to read 1 KB than 1024 bytes and 1 MB than 1048576 bytes. Converts
 * number of bytes to human readable number by taking the number of that unit
 * that the bytes will go into it. Supports TB value.
 *
 * Please note that integers in PHP are limited to 32 bits, unless they are on
 * 64 bit architecture, then they have 64 bit size. If you need to place the
 * larger size then what PHP integer type will hold, then use a string. It will
 * be converted to a double, which should always have 64 bit length.
 *
 * Technically the correct unit names for powers of 1024 are KiB, MiB etc.
 *
 * @since 2.3.0
 *
 * @param int|string $bytes    Number of bytes. Note max integer size for integers.
 * @param int        $decimals Optional. Precision of number of decimal places. Default 0.
 * @return string|false Number string on success, false on failure.
 */
function size_format($bytes, $decimals = 0)
{
    $quant = array(
        /* translators: Unit symbol for terabyte. */
        _x('TB', 'unit symbol') => TB_IN_BYTES,
        /* translators: Unit symbol for gigabyte. */
        _x('GB', 'unit symbol') => GB_IN_BYTES,
        /* translators: Unit symbol for megabyte. */
        _x('MB', 'unit symbol') => MB_IN_BYTES,
        /* translators: Unit symbol for kilobyte. */
        _x('KB', 'unit symbol') => KB_IN_BYTES,
        /* translators: Unit symbol for byte. */
        _x('B', 'unit symbol') => 1,
    );
    if (0 === $bytes) {
        /* translators: Unit symbol for byte. */
        return number_format_i18n(0, $decimals) . ' ' . _x('B', 'unit symbol');
    }
    foreach ($quant as $unit => $mag) {
        if ((float) $bytes >= $mag) {
            return number_format_i18n($bytes / $mag, $decimals) . ' ' . $unit;
        }
    }
    return false;
}

WordPress Version: 5.5

/**
 * Convert number of bytes largest unit bytes will fit into.
 *
 * It is easier to read 1 KB than 1024 bytes and 1 MB than 1048576 bytes. Converts
 * number of bytes to human readable number by taking the number of that unit
 * that the bytes will go into it. Supports TB value.
 *
 * Please note that integers in PHP are limited to 32 bits, unless they are on
 * 64 bit architecture, then they have 64 bit size. If you need to place the
 * larger size then what PHP integer type will hold, then use a string. It will
 * be converted to a double, which should always have 64 bit length.
 *
 * Technically the correct unit names for powers of 1024 are KiB, MiB etc.
 *
 * @since 2.3.0
 *
 * @param int|string $bytes    Number of bytes. Note max integer size for integers.
 * @param int        $decimals Optional. Precision of number of decimal places. Default 0.
 * @return string|false Number string on success, false on failure.
 */
function size_format($bytes, $decimals = 0)
{
    $quant = array(
        /* translators: Unit symbol for terabyte. */
        _x('TB', 'unit symbol') => TB_IN_BYTES,
        /* translators: Unit symbol for gigabyte. */
        _x('GB', 'unit symbol') => GB_IN_BYTES,
        /* translators: Unit symbol for megabyte. */
        _x('MB', 'unit symbol') => MB_IN_BYTES,
        /* translators: Unit symbol for kilobyte. */
        _x('KB', 'unit symbol') => KB_IN_BYTES,
        /* translators: Unit symbol for byte. */
        _x('B', 'unit symbol') => 1,
    );
    if (0 === $bytes) {
        /* translators: Unit symbol for byte. */
        return number_format_i18n(0, $decimals) . ' ' . _x('B', 'unit symbol');
    }
    foreach ($quant as $unit => $mag) {
        if (doubleval($bytes) >= $mag) {
            return number_format_i18n($bytes / $mag, $decimals) . ' ' . $unit;
        }
    }
    return false;
}

WordPress Version: 4.6

/**
 * Convert number of bytes largest unit bytes will fit into.
 *
 * It is easier to read 1 KB than 1024 bytes and 1 MB than 1048576 bytes. Converts
 * number of bytes to human readable number by taking the number of that unit
 * that the bytes will go into it. Supports TB value.
 *
 * Please note that integers in PHP are limited to 32 bits, unless they are on
 * 64 bit architecture, then they have 64 bit size. If you need to place the
 * larger size then what PHP integer type will hold, then use a string. It will
 * be converted to a double, which should always have 64 bit length.
 *
 * Technically the correct unit names for powers of 1024 are KiB, MiB etc.
 *
 * @since 2.3.0
 *
 * @param int|string $bytes    Number of bytes. Note max integer size for integers.
 * @param int        $decimals Optional. Precision of number of decimal places. Default 0.
 * @return string|false False on failure. Number string on success.
 */
function size_format($bytes, $decimals = 0)
{
    $quant = array('TB' => TB_IN_BYTES, 'GB' => GB_IN_BYTES, 'MB' => MB_IN_BYTES, 'KB' => KB_IN_BYTES, 'B' => 1);
    if (0 === $bytes) {
        return number_format_i18n(0, $decimals) . ' B';
    }
    foreach ($quant as $unit => $mag) {
        if (doubleval($bytes) >= $mag) {
            return number_format_i18n($bytes / $mag, $decimals) . ' ' . $unit;
        }
    }
    return false;
}

WordPress Version: 4.4

/**
 * Convert number of bytes largest unit bytes will fit into.
 *
 * It is easier to read 1 kB than 1024 bytes and 1 MB than 1048576 bytes. Converts
 * number of bytes to human readable number by taking the number of that unit
 * that the bytes will go into it. Supports TB value.
 *
 * Please note that integers in PHP are limited to 32 bits, unless they are on
 * 64 bit architecture, then they have 64 bit size. If you need to place the
 * larger size then what PHP integer type will hold, then use a string. It will
 * be converted to a double, which should always have 64 bit length.
 *
 * Technically the correct unit names for powers of 1024 are KiB, MiB etc.
 *
 * @since 2.3.0
 *
 * @param int|string $bytes    Number of bytes. Note max integer size for integers.
 * @param int        $decimals Optional. Precision of number of decimal places. Default 0.
 * @return string|false False on failure. Number string on success.
 */
function size_format($bytes, $decimals = 0)
{
    $quant = array('TB' => TB_IN_BYTES, 'GB' => GB_IN_BYTES, 'MB' => MB_IN_BYTES, 'kB' => KB_IN_BYTES, 'B' => 1);
    foreach ($quant as $unit => $mag) {
        if (doubleval($bytes) >= $mag) {
            return number_format_i18n($bytes / $mag, $decimals) . ' ' . $unit;
        }
    }
    return false;
}

WordPress Version: 4.2

/**
 * Convert number of bytes largest unit bytes will fit into.
 *
 * It is easier to read 1 kB than 1024 bytes and 1 MB than 1048576 bytes. Converts
 * number of bytes to human readable number by taking the number of that unit
 * that the bytes will go into it. Supports TB value.
 *
 * Please note that integers in PHP are limited to 32 bits, unless they are on
 * 64 bit architecture, then they have 64 bit size. If you need to place the
 * larger size then what PHP integer type will hold, then use a string. It will
 * be converted to a double, which should always have 64 bit length.
 *
 * Technically the correct unit names for powers of 1024 are KiB, MiB etc.
 *
 * @since 2.3.0
 *
 * @param int|string $bytes    Number of bytes. Note max integer size for integers.
 * @param int        $decimals Optional. Precision of number of decimal places. Default 0.
 * @return string|false False on failure. Number string on success.
 */
function size_format($bytes, $decimals = 0)
{
    $quant = array(
        // ========================= Origin ====
        'TB' => 1099511627776,
        // pow( 1024, 4)
        'GB' => 1073741824,
        // pow( 1024, 3)
        'MB' => 1048576,
        // pow( 1024, 2)
        'kB' => 1024,
        // pow( 1024, 1)
        'B' => 1,
    );
    foreach ($quant as $unit => $mag) {
        if (doubleval($bytes) >= $mag) {
            return number_format_i18n($bytes / $mag, $decimals) . ' ' . $unit;
        }
    }
    return false;
}

WordPress Version: 4.1

/**
 * Convert number of bytes largest unit bytes will fit into.
 *
 * It is easier to read 1kB than 1024 bytes and 1MB than 1048576 bytes. Converts
 * number of bytes to human readable number by taking the number of that unit
 * that the bytes will go into it. Supports TB value.
 *
 * Please note that integers in PHP are limited to 32 bits, unless they are on
 * 64 bit architecture, then they have 64 bit size. If you need to place the
 * larger size then what PHP integer type will hold, then use a string. It will
 * be converted to a double, which should always have 64 bit length.
 *
 * Technically the correct unit names for powers of 1024 are KiB, MiB etc.
 *
 * @since 2.3.0
 *
 * @param int|string $bytes    Number of bytes. Note max integer size for integers.
 * @param int        $decimals Optional. Precision of number of decimal places. Default 0.
 * @return string|false False on failure. Number string on success.
 */
function size_format($bytes, $decimals = 0)
{
    $quant = array(
        // ========================= Origin ====
        'TB' => 1099511627776,
        // pow( 1024, 4)
        'GB' => 1073741824,
        // pow( 1024, 3)
        'MB' => 1048576,
        // pow( 1024, 2)
        'kB' => 1024,
        // pow( 1024, 1)
        'B ' => 1,
    );
    foreach ($quant as $unit => $mag) {
        if (doubleval($bytes) >= $mag) {
            return number_format_i18n($bytes / $mag, $decimals) . ' ' . $unit;
        }
    }
    return false;
}

WordPress Version: 4.0

/**
 * Convert number of bytes largest unit bytes will fit into.
 *
 * It is easier to read 1kB than 1024 bytes and 1MB than 1048576 bytes. Converts
 * number of bytes to human readable number by taking the number of that unit
 * that the bytes will go into it. Supports TB value.
 *
 * Please note that integers in PHP are limited to 32 bits, unless they are on
 * 64 bit architecture, then they have 64 bit size. If you need to place the
 * larger size then what PHP integer type will hold, then use a string. It will
 * be converted to a double, which should always have 64 bit length.
 *
 * Technically the correct unit names for powers of 1024 are KiB, MiB etc.
 *
 * @since 2.3.0
 *
 * @param int|string $bytes    Number of bytes. Note max integer size for integers.
 * @param int        $decimals Optional. Precision of number of decimal places. Default 0.
 * @return bool|string False on failure. Number string on success.
 */
function size_format($bytes, $decimals = 0)
{
    $quant = array(
        // ========================= Origin ====
        'TB' => 1099511627776,
        // pow( 1024, 4)
        'GB' => 1073741824,
        // pow( 1024, 3)
        'MB' => 1048576,
        // pow( 1024, 2)
        'kB' => 1024,
        // pow( 1024, 1)
        'B ' => 1,
    );
    foreach ($quant as $unit => $mag) {
        if (doubleval($bytes) >= $mag) {
            return number_format_i18n($bytes / $mag, $decimals) . ' ' . $unit;
        }
    }
    return false;
}

WordPress Version: 3.7

/**
 * Convert number of bytes largest unit bytes will fit into.
 *
 * It is easier to read 1kB than 1024 bytes and 1MB than 1048576 bytes. Converts
 * number of bytes to human readable number by taking the number of that unit
 * that the bytes will go into it. Supports TB value.
 *
 * Please note that integers in PHP are limited to 32 bits, unless they are on
 * 64 bit architecture, then they have 64 bit size. If you need to place the
 * larger size then what PHP integer type will hold, then use a string. It will
 * be converted to a double, which should always have 64 bit length.
 *
 * Technically the correct unit names for powers of 1024 are KiB, MiB etc.
 * @link http://en.wikipedia.org/wiki/Byte
 *
 * @since 2.3.0
 *
 * @param int|string $bytes Number of bytes. Note max integer size for integers.
 * @param int $decimals Precision of number of decimal places. Deprecated.
 * @return bool|string False on failure. Number string on success.
 */
function size_format($bytes, $decimals = 0)
{
    $quant = array(
        // ========================= Origin ====
        'TB' => 1099511627776,
        // pow( 1024, 4)
        'GB' => 1073741824,
        // pow( 1024, 3)
        'MB' => 1048576,
        // pow( 1024, 2)
        'kB' => 1024,
        // pow( 1024, 1)
        'B ' => 1,
    );
    foreach ($quant as $unit => $mag) {
        if (doubleval($bytes) >= $mag) {
            return number_format_i18n($bytes / $mag, $decimals) . ' ' . $unit;
        }
    }
    return false;
}