Home  >  Article  >  Backend Development  >  PHP格式化(文件)储存数据大小(SIZE)显示

PHP格式化(文件)储存数据大小(SIZE)显示

WBOY
WBOYOriginal
2016-06-13 12:12:45928browse

PHP格式化(文件)存储数据大小(SIZE)显示

有时候我们需要在网页上显示某个文件的大小,或者是其它数据的大小数字。

这个数字往往从跨度很大,如果以B为单位的话可能是个位,如果1G则长达1073741824的数字,这个时候我们就需要根据大小来格式化,比如小于1K则以B为单位显示,小于1M则以KB为单位显示,小于1G则以MB为单位显示,以此类推...

格式化函数参考如下:

<span style="color: #008000;">//</span><span style="color: #008000;">格式化size显示</span><span style="color: #0000ff;">function</span> formatSize(<span style="color: #800080;">$b</span>,<span style="color: #800080;">$times</span>=0<span style="color: #000000;">){</span>    <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$b</span>>1024<span style="color: #000000;">){        </span><span style="color: #800080;">$temp</span>=<span style="color: #800080;">$b</span>/1024<span style="color: #000000;">;        </span><span style="color: #0000ff;">return</span> formatSize(<span style="color: #800080;">$temp</span>,<span style="color: #800080;">$times</span>+1<span style="color: #000000;">);    }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{        </span><span style="color: #800080;">$unit</span>='B'<span style="color: #000000;">;        </span><span style="color: #0000ff;">switch</span>(<span style="color: #800080;">$times</span><span style="color: #000000;">){            </span><span style="color: #0000ff;">case</span> '0':<span style="color: #800080;">$unit</span>='B';<span style="color: #0000ff;">break</span><span style="color: #000000;">;            </span><span style="color: #0000ff;">case</span> '1':<span style="color: #800080;">$unit</span>='KB';<span style="color: #0000ff;">break</span><span style="color: #000000;">;            </span><span style="color: #0000ff;">case</span> '2':<span style="color: #800080;">$unit</span>='MB';<span style="color: #0000ff;">break</span><span style="color: #000000;">;            </span><span style="color: #0000ff;">case</span> '3':<span style="color: #800080;">$unit</span>='GB';<span style="color: #0000ff;">break</span><span style="color: #000000;">;            </span><span style="color: #0000ff;">case</span> '4':<span style="color: #800080;">$unit</span>='TB';<span style="color: #0000ff;">break</span><span style="color: #000000;">;            </span><span style="color: #0000ff;">case</span> '5':<span style="color: #800080;">$unit</span>='PB';<span style="color: #0000ff;">break</span><span style="color: #000000;">;            </span><span style="color: #0000ff;">case</span> '6':<span style="color: #800080;">$unit</span>='EB';<span style="color: #0000ff;">break</span><span style="color: #000000;">;            </span><span style="color: #0000ff;">case</span> '7':<span style="color: #800080;">$unit</span>='ZB';<span style="color: #0000ff;">break</span><span style="color: #000000;">;            </span><span style="color: #0000ff;">default</span>: <span style="color: #800080;">$unit</span>='单位未知'<span style="color: #000000;">;        }        </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">sprintf</span>('%.2f',<span style="color: #800080;">$b</span>).<span style="color: #800080;">$unit</span><span style="color: #000000;">;    }}</span>

调用:

<span style="color: #0000ff;">echo</span> formatSize('20667564');

结果是:

19.71MB

 

说明:

其中参数$b是以B为单位的数字,$times是用来标识这个函数递归了多少次的。

其中不常用的单位TB、PB、EB、ZB请参考下面的备注(来源于网络):

1bit(这个比特表示一个二进制数字)
1Byte(这个单词音译过来也叫“比特”但是表示一个十六进制的数字)
1B=1Byte=8bit
1 kB = 1024 B (kB - kilobyte) 千
1 MB = 1024 kB (MB - megabyte) 兆
1 GB = 1024 MB (GB - gigabyte) 吉
1 TB = 1024 GB (TB - terabyte) 太
1 PB = 1024 TB (PB - petabyte) 拍
1 EB = 1024 PB (EB - eksabyte) 艾
1 ZB = 1024 EB (ZB - zettabyte) 皆
1 YB = 1024 ZB (YB - yottabyte) 佑
1 BB = 1024 YB (BB - brontobyte)
yotta, 尧[它], Y. 10^21,
zetta, 泽[它], Z. 10^18,
exa, 艾[可萨], E. 10^15,
peta, 拍[它], P. 10^12,
tera, 太[拉], T. 10^9,
giga, 吉[咖], G. 10^6,
mega, 兆,M. 10^3 

Statement:
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