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 to compile the statistics file size function.

I learned the fread reading command and used the filesize function to compile the statistics file size function.

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:
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!
Related Article

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.