PHP implements the original method of converting Byte to KB, MB, GB, and TB.
This example describes how to convert the number of bytes in PHP to KB, MB, GB, or TB. We will share this with you for your reference. The details are as follows:
The previous section describes how to convert the number of bytes in java to KB, MB, GB, and TB. Here, PHP is used to implement this function. The code is very 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 (1024, 2) {$ p = 1; $ format = 'kb ';} if ($ num> = pow (1024, 2) & $ num <pow (1024, 3) {$ p = 2; $ format = 'mb ';} if ($ num> = pow (1024, 3) & $ num <pow (1024, 4) {$ p = 3; $ format = 'gb ';} if ($ num> = pow (1024, 4) & $ num <pow (1024, 5) {$ p = 3; $ forma T = 'tb';} $ num/= pow (1024, $ p); return number_format ($ num, 3 ). ''. $ format;} echo "test result from www.www.jb51.net: <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/>";?>
The running result is as follows:
PS: here we recommend several online tools for your reference:
Online universal unit calculator flash Edition("Data Storage" is the unit conversion described in this article ):
Http://tools.jb51.net/zhuanhuanqi/all_zhuanhuanqi
Scientific calculator online use _ advanced calculator online computing:
Http://tools.jb51.net/jisuanqi/jsqkexue
Online hard disk partition calculator:
Http://tools.jb51.net/jisuanqi/onlinediskjsq