PHP implementation of format file data size display method

Source: Internet
Author: User
Tags php file upload

sometimes we need to show the size of a file on a Web page, or the size or number of other data.

This number is often from a large span, if the unit B is likely to be bit, if 1G is up to 1073741824 of the number, this time we need to format according to size, such as less than 1K is displayed in units B, less than 1M is displayed in kilobytes, Less than 1G is displayed in megabytes, etc. ...

The Format function reference is as follows:

Format size Display

function Formatsize ($b, $times =0) {

if ($b >1024) {

$temp = $b/1024;

Return Formatsize ($temp, $times + 1);

}else{

$unit = ' B ';

Switch ($times) {

Case ' 0 ': $unit = ' B ';

Case ' 1 ': $unit = ' KB ';

Case ' 2 ': $unit = ' MB ';

Case ' 3 ': $unit = ' GB ';

Case ' 4 ': $unit = ' TB ';

Case ' 5 ': $unit = ' PB ';

Case ' 6 ': $unit = ' EB ';

Case ' 7 ': $unit = ' ZB ';

Default: $unit = ' unit unknown ';

}

return sprintf ('%.2f ', $b). $unit;

}

}

Description:

where the parameter $b is a number in B, $times is used to identify how many times the function is recursive.

the non-commonly used units TB, PB, EB, ZB please refer to the following notes (from the network):

1bit (This bit represents a binary number)

1Byte (This word is also called "bit") but it represents a hexadecimal number.

1b=1byte=8bit

1 KB = 1024x768 B (kb-kilobyte) thousand

1 MB = KB (mb-megabyte) trillion

1 GB = 1024x768 (gb-gigabyte)

1 TB = 1024x768 (tb-terabyte) Too

1 PB = 1024x768 TB (pb-petabyte) Pat

1 EB = 1024x768 PB (eb-eksabyte) AI

1 ZB = Zb-zettabyte (EB)

1 YB = 1024x768 ZB (yb-yottabyte) Woo

1 BB = YB (bb-brontobyte)

Yotta, Yao [it], Y. 10^21,

Zetta, ze [it], Z. 10^18,

Exa, Ai [sa], E. 10^15,

PETA, Pat [it], P. 10^12,

Tera, too [pull], T. 10^9,

Giga, Ji [Cafe], G. 10^6,

Mega, Trillion, M. 10^3

Reference: http://www.lai18.com/content/368948.html

Kelinlin > "php Practical Problem Solving Case"

    • PHP to determine whether a request is an AJAX request or a normal request method

    • How to modify PHP's memory_limit limit

    • PHP File Upload Problem Summary--file size detection and large file upload processing

    • How PHP replicates directories and subdirectories recursively

    • How to delete an entire directory through Directoryiterator in PHP

    • PHP calculation of the file space under the specified directory method

    • PHP simple way to get video preview map

    • PHP interface Data encryption, decryption, authentication signature

    • How to query the Express information in PHP

    • Php method for SMTP sending mail based on socket

PHP implementation of format file data size display method

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.