Php statistics file size, measured in GB, MB, KB, and B
Source: Internet
Author: User
I learned the fread reading command and used the filesize function. when I knew that this function was useful, I used it to compile the statistics file size function and used the filesize () function command to collect statistics on the file size, requirements: 1, with one output in 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:
The 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:
The 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!
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.