PHP implementation of format file data size Display Method _php Tutorial

Source: Internet
Author: User

PHP implementation of format file data size display method

This article mainly introduced the PHP implementation format file data size display method, through a custom function to achieve the exact format of the file size, has a certain reference value, the need for friends can refer to the following

This article describes how PHP implements the format file data size display. Share to everyone for your reference. The specific analysis is as follows:

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 a bit, if 1G is up to 1073741824 of the number, this time we need to format according to the 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:

The code is as follows:

Format size Display
function Formatsize ($b, $times =0) {
if ($b >1024) {
$temp = $b/1024;
Return Formatsize ($temp, $times + 1);
$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;


The code is as follows:

echo formatsize (' 20667564 ');

The result is:



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.
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

I hope this article is helpful to everyone's PHP programming. true techarticle PHP Implementation of format file data size display method This article mainly introduces the PHP implementation of format file data size display method, through a custom function for file size ...

  • 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: 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.