PHP implements the format file data size display method,
This article describes how PHP implements the format file data size display. Share to everyone for your reference. The specific analysis is as follows:
Sometimes we need to show the size of a file on a Web page, or the size or number of other data.
This number is often from a large span, if the unit B is a bit, if 1G is up to 1073741824 of the number, this time we need to format according to the size, such as less than 1K is displayed in units B, less than 1M is displayed in kilobytes, less than 1G is displayed in megabytes, etc...
The Format function reference is as follows:
Copy the Code code as follows:
Format size Display
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 ';
Case ' 1 ': $unit = ' KB ';
Case ' 2 ': $unit = ' MB ';
Case ' 3 ': $unit = ' GB ';
Case ' 4 ': $unit = ' TB ';
Case ' 5 ': $unit = ' PB ';
Case ' 6 ': $unit = ' EB ';
Case ' 7 ': $unit = ' ZB ';
Default: $unit = ' unit unknown ';
}
return sprintf ('%.2f ', $b). $unit;
}
}
Call:
Copy the Code code as follows: Echo formatsize (' 20667564 ');
The result is:
19.71MB
Description
Where the parameter $b is a number in B, $times is used to identify how many times the function is recursive.
The non-commonly used units TB, PB, EB, ZB please refer to the following notes (from the network):
1bit (this bit represents a binary number)
1Byte (this word is also called "bit") but it represents a hexadecimal number.
1b=1byte=8bit
1 KB = 1024x768 B (kb-kilobyte) thousand
1 MB = KB (mb-megabyte) trillion
1 GB = 1024x768 (gb-gigabyte)
1 TB = 1024x768 (tb-terabyte) too
1 PB = 1024x768 TB (pb-petabyte) Pat
1 EB = 1024x768 PB (eb-eksabyte) AI
1 ZB = Zb-zettabyte (EB)
1 YB = 1024x768 ZB (yb-yottabyte) Woo
1 BB = YB (bb-brontobyte)
Yotta, Yao [it], Y. 10^21,
Zetta, ze [it], Z. 10^18,
Exa, Ai [sa], E. 10^15,
PETA, Pat [it], P. 10^12,
Tera, too [pull], T. 10^9,
Giga, Ji [Cafe], G. 10^6,
Mega, trillion, M. 10^3
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/936802.html www.bkjia.com true http://www.bkjia.com/PHPjc/936802.html techarticle PHP Implementation of the format file data size display method, this article describes the implementation of PHP format file data size display method. Share to everyone for your reference. The specific analysis is as follows: ...