php statistics file size output in GB MB KB B
- 2020-05-05 11:04:01
- OfStack
Use the filesize () function command to achieve a file size statistics, requirements: 1, to GB, MB, KB, B, an output; 2. The order of magnitude must be greater than 1 and less than 1024, with two decimal places reserved;
Start work:
Demo effect:
view sourceprint?1.85GB
2.70GB
In my mind, I went to the PHP tutorial on PHP website and found a simpler and more effective way (So Peifu)
The code is as follows:
Demo effect:
1.85GB
2.7GB
Of course there are more practices, but this method should be the easiest, the fastest bar, I believe you also have other methods, look forward to your share!
Start work:
$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.85GB
2.70GB
In my mind, I went to the PHP tutorial on PHP website and found a simpler and more effective way (So Peifu)
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.85GB
2.7GB
Of course there are more practices, but this method should be the easiest, the fastest bar, I believe you also have other methods, look forward to your share!