Use the FileSize () function command to implement statistics for file size, requiring: 1, one output in GB, MB, KB, B, 2. The order of magnitude must be greater than 1 less than 1024 and retain two decimal places;
Commencement of construction:
Copy Code code 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);
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, on the PHP website of PHP tutorial on the results found a simpler and more effective method (so Peifu)
The code is as follows:
Copy Code code 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.85GB
2.7GB
Of course, there are more practices, but this method is supposed to be the simplest, the fastest, I believe you also have other methods, look forward to your sharing!