Home > Backend Development > PHP Tutorial > php统计文件大小,以GB、MB、KB、B输出_PHP

php统计文件大小,以GB、MB、KB、B输出_PHP

WBOY
Release: 2016-06-01 12:16:54
Original
1104 people have browsed it

使用filesize()函数命令实现文件大小的统计,要求:1,以GB、MB、KB、B中的一个输出;2.数量级必须大于1小于1024,并保留两位小数;
  开始动工:
复制代码 代码如下:
$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--;
}

演示效果:
view sourceprint?1.85GB
2.70GB
  得意间,上PHP官网的PHP教程上看,结果找到了一个更简单更有效的方法(So Peifu)
  代码如下:
复制代码 代码如下:
function format_bytes($size) {
$units = array(' B', ' KB', ' MB', ' GB', ' TB');
for ($i = 0; $size >= 1024 && $i return round($size, 2).$units[$i];
}

演示效果:
1.85GB
2.7GB
  当然还有更多的做法,但这种方法是应该是最简单的,最快的了吧,相信你也有别的方法,期待你的分享!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template