PHP _ PHP Tutorial

Source: Internet
Author: User
PHP allows you to display the data size of a formatted file. PHP: how to display the size of formatted file data this article mainly introduces how to display the size of formatted file data in PHP, use a custom function to display the file size in PHP format.

This article mainly introduces how to display the data size of a formatted file in PHP. Using a custom function to accurately format the file size is of reference value, for more information, see

This example describes how to display the size of formatted file data in PHP. Share it with you for your reference. The specific analysis is as follows:

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:

The code is as follows:


// Format the size display
Function formatSize ($ B, $ times = 0 ){
If ($ B & gt; 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:

The code is as follows:

Echo formatSize ('000000 ');


The result is:

19.71 MB

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

I hope this article will help you with php programming.

This article describes how to display the data size of a formatted file in PHP. you can use a custom function to display the file size...

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.