size_format() - WordPress 函数参考中文文档

返回介绍

size_format()

发布于 2017-09-11 字数3079 浏览 912 评论 0

size_format( int|string $bytes,  int $decimals )

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.


参数

$bytes

(int|string)
(Required)
Number of bytes. Note max integer size for integers.

$decimals

(int)
(Optional)
Precision of number of decimal places. Default 0.


返回值

(string|false) False on failure. Number string on success.


源代码

File: wp-includes/functions.php

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

更新日志

Version 描述
2.3.0 Introduced.

相关函数

Uses

  • wp-includes/functions.php:
    number_format_i18n()

Used By

  • wp-admin/includes/template.php:
    wp_import_upload_form()
  • wp-admin/includes/media.php:
    attachment_submitbox_metadata()
  • wp-admin/includes/media.php:
    media_upload_form()
  • wp-includes/media.php:
    wp_prepare_attachment_for_js()
  • wp-includes/media-template.php:
    wp_print_media_templates()

User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note

    Contributed by Codex

    Example

    Display the size of a file

    
    $file_size = 1229; // filesize in bytes
    echo size_format( $file_size, $decimals = 2 );
    // displays "1.20 kB"
    

上一篇:site_url()

下一篇:smilies_init()

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!