Home >Backend Development >PHP Tutorial > php有关问题(急功近利)

php有关问题(急功近利)

WBOY
WBOYOriginal
2016-06-13 13:51:08942browse

php问题(急功近利)
用PHP写一个格式化显示文件大小的函数。即显示为345b,123K,78M,1.2G,这就是说,超过1K的用K为单位显示,超过1M的以M为单位显示。

------解决方案--------------------

PHP code
<?php function getFileSize($file_name){
        $K = 1024;
        $M = 1024*$K;
        $G = 1024*$M;
        $file_size = filesize($file_name);
        if($file_size>$G){
            $file_size = ($file_size / $G)." G";
        }elseif($file_size>$M){
            $file_size = ($file_size / $M) ." M";
        }elseif($file_size > $K){
            $file_size = ($file_size /$K). " K";
        }else{
            $file_size = $file_size. " byte";
        }
        return $file_size;
    }
    echo getFileSize("new_file.html");
?> <div class="clear">
                 
              
              
        
            </div>
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