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 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.
,