Mission Impossible 4 PHP statistical file size in GB, MB, KB, b output

Source: Internet
Author: User
Tags php website
Using the FileSize () function command to achieve file size statistics, requires: 1, in GB, MB, KB, b in one output; 2. The order of magnitude must be greater than 1 and less than 1024, and two decimal places are reserved;
Commenced construction:

Copy the Code code as follows:


$len = FileSize ("1.RMVB");
$i = 4;
while ($i) {
if ($out = $len/pow (1024x768, $i)) >1.0| | $i ==1) {
Switch ($i) {
Case 4: {printf ("%.2f TB", $out);
Case 3: {printf ("%.2f GB", $out);
Case 2: {printf ("%.2f MB", $out);
Case 1: {printf ("%.2f KB", $out);
}
Break
}
$i--;
}


Demo Effect:
View SOURCEPRINT?1.85GB
2.70GB
Proud room, on the PHP website PHP tutorial See, the results found a simpler and more effective method (so Peifu)
The code is as follows:

Copy the Code code as follows:


function Format_bytes ($size) {
$units = Array (' B ', ' KB ', ' MB ', ' GB ', ' TB ');
for ($i = 0; $size >= && $i < 4; $i + +) $size/= 1024;
Return round ($size, 2). $units [$i];
}


Demo Effect:
1.85GB
2.7GB
Of course there are more ways, but this method is the simplest, the fastest, I believe you have other ways, look forward to your sharing!

The above describes the Mission Impossible 4 PHP statistical file size, in GB, MB, KB, b output, including the Mission Impossible 4 aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

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