This article mainly introduces the PHP implementation of byte-byte conversion to KB, MB, GB, TB method, combined with an instance of PHP for the byte count of the conversion operation of the specific implementation method, involving the use of PHP mathematical operations related functions, the need for friends can refer to the next
This article describes the PHP implementation of byte bytes converted to KB, MB, GB, TB methods. Share to everyone for your reference, as follows:
The Java implementation of byte-to-KB, MB, GB, and TB methods is described earlier, and PHP is used to implement this function instead. The code is simple:
<?phpfunction GetFileSize ($num) { $p = 0; $format = ' bytes '; if ($num >0 && $num <1024) { $p = 0; Return Number_format ($num). ' '. $format; } if ($num >=1024 && $num <pow (1024x768, 2)) { $p = 1; $format = ' KB '; } if ($num >=pow (1024x768, 2) && $num <pow (1024x768, 3)) { $p = 2; $format = ' MB '; } if ($num >=pow (1024x768, 3) && $num <pow (1024x768, 4)) { $p = 3; $format = ' GB '; } if ($num >=pow (1024x768, 4) && $num <pow (1024x768, 5)) { $p = 3; $format = ' TB '; } $num/= Pow (1024x768, $p); Return Number_format ($num, 3). ' '. $format;} echo "www.jb51.net test results from the home of the script:<br/>"; Echo getfilesize (200). " <br/> "; Echo getfilesize (20000)." <br/> "; Echo getfilesize (2000000)." <br/> "; Echo getfilesize (200000000)." <br/> "; Echo getfilesize (20000000000)." <br/> "; Echo getfilesize (2000000000000)." <br/> ";? >
Running results such as: