Home  >  Article  >  php教程  >  文件大小单位格式化,自动b,kb,mb,gb,tb

文件大小单位格式化,自动b,kb,mb,gb,tb

WBOY
WBOYOriginal
2016-06-07 11:45:141507browse

上传的文件大小单位是byte
使用本函数可对文件大小格式化,转换成合适的单位,显示到模板中。
/**<br>  * 文件大小单位格式化<br>  * @param $bytes 文件实际大小,单位byte<br>  * @param $prec 转换后精确度,默认精确到小数点后两位<br>  * @return 转换后的大小+单位的字符串<br>  */<br> function fsizeformat($bytes,$prec=2){<br>     $rank=0;<br>     $size=$bytes;<br>     $unit="B";<br>     while($size>1024){<br>         $size=$size/1024;<br>         $rank++;<br>     }<br>     $size=round($size,$prec);<br>     switch ($rank){<br>         case "1":<br>             $unit="KB";<br>             break;<br>         case "2":<br>             $unit="MB";<br>             break;<br>         case "3":<br>             $unit="GB";<br>             break;<br>         case "4":<br>             $unit="TB";<br>             break;<br>         default :<br>             <br>     }<br>     return $size." ".$unit;<br> }使用方法:将该函数添加到 项目Common/common.php 中,清空runtime后即可生效。你可以在控制器或者模板中直接调用,在模板中调用示例:<p>文件大小:{$filesize|fsizeformat}</p>

AD:真正免费,域名+虚机+企业邮箱=0元

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