根据文件字节数转换为KB、MB、GB

代码如下
function getFileSize( $size = 0 ){
     
        if( empty( $size ) ){
     
            return 0;
        }

        if( !is_numeric( $size ) ){
     
            return 0;
        }

        if( $size < 1024 ){
     
            return 0;
        }

        $unit = [ 'B' , 'KB' , 'MB' , 'GB' ];

        for ( $i=0; $size >= 1024 && $i <= 4 ;$i++){
     
            $size /= 1024;
        }
        return round( $size , 2 ) . $unit[$i];
    }

你可能感兴趣的:(编程语言,PHP)