字节换算功能函数就是把kb转换在mb,gb,tg,eb,pb之类的单位,这个对于我们文件上传是非常的有用的,下面给各位分享一个函数,代码如下:
-
- function conversion($size) {
- $kb = 1024;
- $mb = 1024 * $kb;
- $gb = 1024 * $mb;
- $tb = 1024 * $gb;
- $pb = 1024 * $tb;
- $fb = 1024 * $pb;
- $zb = 1024 * $fb;
- $yb = 1024 * $zb;
- $bb = 1024 * $yb;
-
- if ($size < $kb) {
- return $size . " B";
- } else if ($size < $mb) {
- return round($size / $kb, 2) . " KB";
- } else if ($size < $gb) {
- return round($size / $mb, 2) . " MB";
- } else if ($size < $tb) {
- return round($size / $gb, 2) . " GB";
- } else if ($size < $pb) {
- return round($size / $tb, 2) . " TB";
- } else if ($size < $fb) {
- return round($size / $pb, 2) . " PB";
- } else if ($size < $zb) {
- return round($size / $fb, 2) . " EB";
- } else if ($size < $yb) {
- return round($size / $zb, 2) . " ZB";
- } else {
- return round($size / $bb, 2) . " YB";
- }
- }
|