Use the filesize () function command to calculate the file size. The value 1 must be an output of GB, MB, KB, and B. 2. the order of magnitude must be greater than 1 and less than 1024, and two decimal places must be retained;
Start:
Copy codeThe Code is as follows:
$ Len = filesize ("1. rmvb ");
$ I = 4;
While ($ I ){
If ($ out = $ len/pow (1024, $ I)> 1.0 | $ I = 1 ){
Switch ($ I ){
Case 4: {printf ("%. 2f TB", $ out); break ;}
Case 3: {printf ("%. 2f GB", $ out); break ;}
Case 2: {printf ("%. 2f MB", $ out); break ;}
Case 1: {printf ("%. 2f KB", $ out); break ;}
}
Break;
}
$ I --;
}
Demo effect:
View sourceprint? 1.85 GB
2.70 GB
We are proud to find a simpler and more effective method (So Peifu) on the PHP tutorial on the PHP official website)
The Code is as follows:
Copy codeThe Code is as follows:
Function format_bytes ($ size ){
$ Units = array ('B', 'kb', 'mb', 'gb', 'tb ');
For ($ I = 0; $ size >=1024 & $ I <4; $ I ++) $ size/= 1024;
Return round ($ size, 2). $ units [$ I];
}
Demo effect:
1.85 GB
2.7 GB
Of course there are more practices, but this method should be the simplest and fastest. I believe you have other methods. I look forward to sharing them with you!