企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
| 文件大小单位转换 | | | --- | --- | ``` function file_size_switch($size, $format = 'kb') { $format = strtolower($format); $p = 0; if ($format == 'kb') { $p = 1; } elseif ($format == 'mb') { $p = 2; } elseif ($format == 'gb') { $p = 3; }elseif ($format == 'tb') { $p = 4; } $size /= pow(1024, $p); return number_format($size, 3); } * [file_size_switch(size, format)] [文件大小单位转换,如果用到单位转换少建议不要用函数] * @param [size] [需要转换的文件大小,单位为bytes] * @param [format] [转换后的单位,默认位'kb'] * @return [返回转换后的大小] * Example $size = 1024; #1024 bytes $switch = file_size_swith($size); 输出: 1 #即1kb ``` ``` /** * [sizecount 数据大小单位转换,文件大小单位转换, byte字节大小开始转换] * @param [type] $byte [需要转换的大小] * @return [type] [description] */ function sizecount($byte) { if ($byte < 1024) { return $byte.'byte'; } elseif (($size = round($byte/1024,2)) < 1024) { return $size.'KB'; } elseif (($size = round($byte / (1024*1024),2)) < 1024) { return $size.'MB'; } else { return round($byte / (1024*1024*1024),2).'GB'; } } ```