Php byte Conversion Function-PHP source code

Source: Internet
Author: User
The Byte conversion function is used to convert kb to mb, gb, tg, eb, pb and other units. This function is very useful for file uploading. We will share with you a function below. The Byte conversion function is used to convert kb to mb, gb, tg, eb, pb and other units. This function is very useful for file uploading. We will share with you a function below.

Script ec (2); script

The Code is as follows:

// Byte Conversion
Function conversion ($ size ){
$ Kb = 1024; // 1KB (Kibibyte, kilobytes) = 1024B,
$ Mb = 1024 * $ kb; // 1 MB (Mebibyte, mb, or "MB") = 1024KB,
$ Gb = 1024 * $ mb; // 1 GB (Gigabyte, Jilin byte, also known as "Gigabit") = 1024 MB,
$ Tb = 1024 * $ gb; // 1 TB (Terabyte, trillion bytes, too byte) = 1024 GB,
$ Pb = 1024 * $ tb; // 1 pb (Petabyte, trillion bytes, byte) = 1024 TB,
$ Fb = 1024 * $ pb; // 1EB (Exabyte, million bytes,) = pb,
$ Zb = 1024 * $ fb; // 1ZB (Zettabyte, 100,000 billion bytes, ze bytes) = 1024EB,
$ Yb = 1024 * $ zb; // 1YB (Yottabyte, 0.1 billion,) = 1024ZB,
$ Bb = 1024 * $ yb; // 1BB (Brontobyte, 100 billion bytes) = 1024YB

If ($ size <$ kb ){
Return $ size. "B ";
} Else if ($ size <$ mb ){
Return round ($ size/$ kb, 2). "KB ";
} Else if ($ size <$ gb ){
Return round ($ size/$ mb, 2). "MB ";
} Else if ($ size <$ tb ){
Return round ($ size/$ gb, 2). "GB ";
} Else if ($ size <$ pb ){
Return round ($ size/$ tb, 2). "TB ";
} Else if ($ size <$ fb ){
Return round ($ size/$ pb, 2). "PB ";
} Else if ($ size <$ zb ){
Return round ($ size/$ fb, 2). "EB ";
} Else if ($ size <$ yb ){
Return round ($ size/$ zb, 2). "ZB ";
} Else {
Return round ($ size/$ bb, 2). "YB ";
}

}

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.