php 字节换算函数

字节换算功能函数就是把kb转换在mb,gb,tg,eb,pb之类的单位,这个对于我们文件上传是非常的有用的,下面给各位分享一个函数,代码如下:

  1. //字节换算
  2. function conversion($size) {
  3. $kb = 1024; // 1KB(Kibibyte,千字节)=1024B,
  4. $mb = 1024 * $kb; //1MB(Mebibyte,兆字节,简称“兆”)=1024KB,
  5. $gb = 1024 * $mb; // 1GB(Gigabyte,吉字节,又称“千兆”)=1024MB,
  6. $tb = 1024 * $gb; // 1TB(Terabyte,万亿字节,太字节)=1024GB,
  7. $pb = 1024 * $tb; //1PB(Petabyte,千万亿字节,拍字节)=1024TB,
  8. $fb = 1024 * $pb; //1EB(Exabyte,百亿亿字节,艾字节)=1024PB,
  9. $zb = 1024 * $fb; //1ZB(Zettabyte,十万亿亿字节,泽字节)= 1024EB,
  10. $yb = 1024 * $zb; //1YB(Yottabyte,一亿亿亿字节,尧字节)= 1024ZB,
  11. $bb = 1024 * $yb; //1BB(Brontobyte,一千亿亿亿字节)= 1024YB
  12. if ($size < $kb) {
  13. return $size . " B";
  14. } else if ($size < $mb) {
  15. return round($size / $kb, 2) . " KB";
  16. } else if ($size < $gb) {
  17. return round($size / $mb, 2) . " MB";
  18. } else if ($size < $tb) {
  19. return round($size / $gb, 2) . " GB";
  20. } else if ($size < $pb) {
  21. return round($size / $tb, 2) . " TB";
  22. } else if ($size < $fb) {
  23. return round($size / $pb, 2) . " PB";
  24. } else if ($size < $zb) {
  25. return round($size / $fb, 2) . " EB";
  26. } else if ($size < $yb) {
  27. return round($size / $zb, 2) . " ZB";
  28. } else {
  29. return round($size / $bb, 2) . " YB";
  30. }
  31. }