The
php has a system-by-File size function, which is filesize (), but this function is in bytes, in some cases, we need to be very intuitive to understand a file size, not only need byte B this unit, but also need KB,MB,GB, Even bigger TB,PB, so we need to write some functions ourselves to format the output of filesize ()
For example, encountered a very large file has 49957289167B, we look at a long string of numbers after the unit is byte B, or do not know the size of this file is a concept, we convert it to GB as a unit, is 46.53GB. You can do this with the following functions: code as follows://conversion Unit function Setupsize ($fileSize) { $size = sprintf ("%u", $fileSize) &nbs P if ($size = = 0) { return ("0 Bytes"); } $sizename = arr Ay ("Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"); return round ($size/pow (1024, ($i = Floor (log ($size, 1024))), 2). $sizename [$i]; function Byte_format ($size, $dec =2) { $a = array ("B", "KB", "MB", "GB", "TB", "PB"); $pos = 0 ; while ($size >= 1024) { $size/= 1024; &NBS P $pos + +; } return round ($size, $dec). " ". $a [$pos]; &NBSP}/* Use:echo format_size (filesize ("fichier")); Example result:13,37 Ko */ function format_size ($o) { $size = array (' Go ' => 1073741824, ' Mo ' => 1048576, ' Ko ' => 1024, ' octets ' => 1); foreach ($size as $k => $v) if ($o >= $v) { &N Bsp if ($k = ' octets ') &NBS P Return round ($o). ' '. $k; return Number_format ($o/$v, 2, ', ', '). ' '. $k; }}/** * file size format * @param integer $size initial file size, in bytes * @return array format File size and array of units in bytes, KB, MB, GB, TB */function file_size_format ($size = 0, $dec = 2) { $unit = Array ("B", "KB", "MB", "GB", "TB", "PB"); $pos = 0; while ($size >= 1024) { $size/= 1024; $pos + +; &n Bsp } $result [' size '] = Round ($size, $DEC); $result [' unit '] = $unit [$pos]; Return $result[' size ']. $result [' Unit ']; } Echo File_size_format (123456789); /** * File size unit format * @param the actual size of the file, the unit byte * @param $prec conversion accuracy, the default precision to two digits after the decimal point * @return Turn Changed size + unit string */ function Fsizeformat ($bytes, $prec =2) { $rank =0; $size = $bytes; nbsp $unit = "B"; while ($size >1024) { $size = $size/1024, $rank + + &NB Sp $size =round ($size, $prec); Switch ($rank) { case "1": $unit = "KB"; break; Case "2": $unit = "MB"; break; Case "3": $unit = "GB"; break; Case "4": &NBSp $unit = "TB"; break; default: } return $size. " ". $unit; &NBSP} /** * capacity format * @param String filename (file path) * @return if file There is a returned formatted string if error returns error message unknown file */ function Sizeformat ($fileName) { //Get File size @ $filesize =filesize ($fileName); //If file does not exist return error message if (false== $filesize) { return ' Unknown File '; } //format file capacity information if ($filesize >= 1073741824) $filesize = Round ($f ilesize/1073741824 * 100)/100. ' GB '; ElseIf ($filesize >= 1048576) $filesize = round ($filesize/1048576 * 100)/100. ' MB '; ElseIf ($filesize >= 1024) $filesize = round ($filesize/1024 * 100)/100. ' KB '; Else $filesize = $filesize. ' Bytes '; return $filesize; }//Test function echo sizeformat (' config.inc.php '); /** * File size format * @param type $filesize /Private Function Sizecount ($filesize) { if ($filesize >= 1073741824) { $filesize = round ($filesize/1073741824 * 100)/10 0. ' GB '; } ELSE if ($filesize >= 1048576) { $filesize = Rou nd ($filesize/1048576 * 100)/100. ' MB '; } ELSE if ($filesize >= 1024) { $filesize = round ( $filesize/1024 * 100)/100. ' KB '; } return $filesize; //The function is mainly based on the number of bytes in the file to determine the unit of statistics that should be selected, that is, a file in a certain unit such as MB, then the file must be less than 1GB, or, of course, GB as a unit, and the file is greater than KB, Otherwise it would have to be in a smaller unit of statistics. The function code is as follows //size () Statistics file size function size ($byte) {&NBsp if ($byte < 1024) { $unit = "B"; } Else if ($byte < 10240) {&NB Sp $byte =ROUND_DP ($byte/1024, 2); $unit = "KB"; } Else if ($byte < 102400) { $byte =ROUND_DP ($byte/1024, 2); &nbs P $unit = "KB"; } Else if ($byte < 1048576) { $byte =ROUND_DP ($byte/1024, 2); &NB Sp $unit = "KB"; } Else if ($byte < 10485760) { $byte =ROUND_DP ($byte/1048576, 2);   ; $unit = "MB"; } Else if ($byte < 104857600) { $byte =ROUND_DP ($byte/1048576,2)   ; $unit = "MB"; } Else if ($byte < 1073741824) { $byte =ROUND_DP ($byte/1048576, 2); &NB Sp $unit = "MB"; } else { $byte =ROUND_DP ($byte/1073741824, 2); $unit = "GB"; } $byte. = $unit; return $byte; }//Auxiliary function round_up (), which is used to choose the number of decimal places, rounded. function ROUND_DP ($num, $DP) { $sh = POW ($DP); Return (Round ($num * $sh)/$sh); So we can get a good amount of it. Statistics the size of any one file, first through the system with the FileSize () function to obtain the file bytes, and then use these functions to convert to the appropriate units on the