Title: size_format
Published: April 25, 2014
Last modified: August 20, 2026

---

# size_format( int|string $bytes, int $decimals ): string|false

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/size_format/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/size_format/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/size_format/?output_format=md#return)
 * [More Information](https://developer.wordpress.org/reference/functions/size_format/?output_format=md#more-information)
 * [Source](https://developer.wordpress.org/reference/functions/size_format/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/size_format/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/size_format/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/size_format/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/size_format/?output_format=md#wp--skip-link--target)

Converts a number of bytes to the largest unit the bytes will fit into.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/size_format/?output_format=md#description)󠁿

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.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/size_format/?output_format=md#parameters)󠁿

 `$bytes`int|stringrequired

Number of bytes. Note max integer size for integers.

`$decimals`intoptional

Precision of number of decimal places. Default 0.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/size_format/?output_format=md#return)󠁿

 string|false Number string on success, false on failure.

## 󠀁[More Information](https://developer.wordpress.org/reference/functions/size_format/?output_format=md#more-information)󠁿

##### 󠀁[Usage:](https://developer.wordpress.org/reference/functions/size_format/?output_format=md#usage)󠁿

    ```php
    // Returns the string '1 KiB'
    <?php $size_to_display = size_format(1024); ?>
    ```

## 󠀁[Source](https://developer.wordpress.org/reference/functions/size_format/?output_format=md#source)󠁿

    ```php
    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;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/functions.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/functions.php#L467)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/functions.php#L467-L501)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/size_format/?output_format=md#related)󠁿

| Uses | Description | 
| [_x()](https://developer.wordpress.org/reference/functions/_x/)`wp-includes/l10n.php` |

Retrieves translated string with gettext context.

  | 
| [number_format_i18n()](https://developer.wordpress.org/reference/functions/number_format_i18n/)`wp-includes/functions.php` |

Converts float number to format based on the locale.

  |

| Used by | Description | 
| [WP_Debug_Data::get_wp_server()](https://developer.wordpress.org/reference/classes/wp_debug_data/get_wp_server/)`wp-admin/includes/class-wp-debug-data.php` |

Gets the WordPress server section of the debug data.

  | 
| [WP_Debug_Data::get_wp_media()](https://developer.wordpress.org/reference/classes/wp_debug_data/get_wp_media/)`wp-admin/includes/class-wp-debug-data.php` |

Gets the WordPress media section of the debug data.

  | 
| [WP_Site_Health::get_test_autoloaded_options()](https://developer.wordpress.org/reference/classes/wp_site_health/get_test_autoloaded_options/)`wp-admin/includes/class-wp-site-health.php` |

Tests the number of autoloaded options.

  | 
| [WP_Site_Health::get_test_available_updates_disk_space()](https://developer.wordpress.org/reference/classes/wp_site_health/get_test_available_updates_disk_space/)`wp-admin/includes/class-wp-site-health.php` |

Tests available disk space for updates.

  | 
| [WP_Debug_Data::get_sizes()](https://developer.wordpress.org/reference/classes/wp_debug_data/get_sizes/)`wp-admin/includes/class-wp-debug-data.php` |

Fetches the sizes of the WordPress directories: `wordpress` (ABSPATH), `plugins`, `themes`, and `uploads`.

  | 
| [upload_is_user_over_quota()](https://developer.wordpress.org/reference/functions/upload_is_user_over_quota/)`wp-admin/includes/ms.php` |

Checks whether a site has used its allotted upload space.

  | 
| [display_space_usage()](https://developer.wordpress.org/reference/functions/display_space_usage/)`wp-admin/includes/ms.php` |

Displays the amount of disk space used by the current site. Not used in core.

  | 
| [wp_import_upload_form()](https://developer.wordpress.org/reference/functions/wp_import_upload_form/)`wp-admin/includes/template.php` |

Outputs the form used by the importers to accept the data to be imported.

  | 
| [multisite_over_quota_message()](https://developer.wordpress.org/reference/functions/multisite_over_quota_message/)`wp-admin/includes/media.php` |

Displays the out of storage quota message in Multisite.

  | 
| [attachment_submitbox_metadata()](https://developer.wordpress.org/reference/functions/attachment_submitbox_metadata/)`wp-admin/includes/media.php` |

Displays non-editable attachment metadata in the publish meta box.

  | 
| [media_upload_form()](https://developer.wordpress.org/reference/functions/media_upload_form/)`wp-admin/includes/media.php` |

Outputs the legacy media upload form.

  | 
| [wpmu_checkAvailableSpace()](https://developer.wordpress.org/reference/functions/wpmu_checkavailablespace/)`wp-admin/includes/ms-deprecated.php` |

Determines if the available space defined by the admin has been exceeded by the user.

  | 
| [wp_prepare_attachment_for_js()](https://developer.wordpress.org/reference/functions/wp_prepare_attachment_for_js/)`wp-includes/media.php` |

Prepares an attachment post object for JS, where it is expected to be JSON-encoded and fit into an Attachment model.

  | 
| [wp_xmlrpc_server::mw_newMediaObject()](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/mw_newmediaobject/)`wp-includes/class-wp-xmlrpc-server.php` |

Uploads a file, following your settings.

  | 
| [wp_print_media_templates()](https://developer.wordpress.org/reference/functions/wp_print_media_templates/)`wp-includes/media-template.php` |

Prints the templates used in the media manager.

  |

[Show 10 more](https://developer.wordpress.org/reference/functions/size_format/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/size_format/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/size_format/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.0.0](https://developer.wordpress.org/reference/since/6.0.0/) | Support for PB, EB, ZB, and YB was added. | 
| [2.3.0](https://developer.wordpress.org/reference/since/2.3.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/size_format/?output_format=md#user-contributed-notes)󠁿

 1.  [Skip to note 2 content](https://developer.wordpress.org/reference/functions/size_format/?output_format=md#comment-content-1455)
 2.   [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/size_format/#comment-1455)
 3. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fsize_format%2F%23comment-1455)
    Vote results for this note: -1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fsize_format%2F%23comment-1455)
 4. **Example**
 5. Display the size of a file
 6.     ```php
        $file_size = 1229; // filesize in bytes
        echo size_format( $file_size, $decimals = 2 );
        // displays "1.20 kB"
        ```
    
 7.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fsize_format%2F%3Freplytocom%3D1455%23feedback-editor-1455)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fsize_format%2F)
before being able to contribute a note or feedback.