PHP format (file) storage data SIZE (SIZE) display

Source: Internet
Author: User
: This article mainly introduces the PHP formatting (file) storage data SIZE (SIZE) display. if you are interested in the PHP Tutorial, please refer to it. Sometimes we need to display the size of a file on the webpage, or the size of other data.

This number is often from a large span. if the unit is B, it may be a single digit. if the unit is 1 GB, it is up to 1073741824 digits. at this time, we need to format the number based on the size, for example, if the value is less than 1 KB, B is displayed. if the value is smaller than 1 MB, KB is displayed. if the value is smaller than 1 GB, MB is displayed...

The formatting function is as follows:

// Format the size to display the function formatSize ($ B, $ times = 0) {if ($ B> 1024) {$ temp = $ B/1024; return formatSize ($ temp, $ times + 1);} else {$ unit = 'B'; switch ($ times) {case '0': $ unit = 'B'; break; case '1': $ unit = 'KB'; break; case '2': $ unit = 'mb'; break; case '3': $ unit = 'GB '; break; case '4': $ unit = 'TB'; break; case '5': $ unit = 'petab'; break; case '6 ': $ unit = 'EB '; break; case '7': $ unit = 'zb'; break; default: $ unit = 'unknown unit';} return sprintf ('%. 2f ', $ B ). $ unit ;}}

Call:

echo formatSize('20667564');

The result is:

19.71MB

Note:

Here, the $ B parameter is a number in the unit of B, and $ times is used to identify the number of recursion of this function.

For the unit TB, PB, EB, and ZB that are not commonly used, refer to the following remarks (from the network ):

1 bit (this bit represents a binary number) 1 Byte (this word is also called "bit" but represents a hexadecimal number)
1B = 1 Byte = 8bit
1 kB = 1024 B (kB-kilobyte) thousand
1 MB = 1024 kB (MB-megabyte) MB
1 GB = 1024 MB (GB-gigabyte) GiB
1 TB = 1024 GB (TB-terabyte) too
1 PB = 1024 TB (PB-petabyte)
1 EB = 1024 PB (EB-eksabyte) AI
1 ZB = 1024 EB (ZB-zettabyte)
1 YB = 1024 ZB (YB-yottabyte) you
1 BB = 1024 YB (BB-brontobyte)
Yotta, Yao [it], Y. 10 ^ 21,
Zetta, ze [it], Z. 10 ^ 18,
Exa, AI [Kisa], E. 10 ^ 15,
Peta, beat [it], P. 10 ^ 12,
Pull, too [pull], T. 10 ^ 9,
Giga, Kyrgyzstan [coffee], G. 10 ^ 6,
Mega, MB, M. 10 ^ 3

The above introduces the PHP formatting (file) storage data SIZE (SIZE) display, including the content, hope to be helpful to friends interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.