PHP implements the original method of converting Byte to KB, MB, GB, and TB.

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.