PHP format (file) storage data size (SIZE) display
Sometimes we need to display the size of a certain file or the size of other data on the web page.
This number often spans a large span. If it is in B, it may be a single digit. If it is 1G, it will be a number up to 1073741824. At this time, we need to format it according to the size. For example, if it is less than 1K, it will be displayed in B. If it is less than 1K, it will be displayed in B. 1M is displayed in KB, less than 1G is displayed in MB, and so on...
The formatting function reference is as follows:
<span>//</span><span>格式化size显示</span> <span>function</span> formatSize(<span>$b</span>,<span>$times</span>=0<span>){ </span> <span>if</span>(<span>$b</span>>1024<span>){ </span><span>$temp</span>=<span>$b</span>/1024<span>; </span><span>return</span> formatSize(<span>$temp</span>,<span>$times</span>+1<span>); }</span><span>else</span><span>{ </span><span>$unit</span>='B'<span>; </span><span>switch</span>(<span>$times</span><span>){ </span><span>case</span> '0':<span>$unit</span>='B';<span>break</span><span>; </span><span>case</span> '1':<span>$unit</span>='KB';<span>break</span><span>; </span><span>case</span> '2':<span>$unit</span>='MB';<span>break</span><span>; </span><span>case</span> '3':<span>$unit</span>='GB';<span>break</span><span>; </span><span>case</span> '4':<span>$unit</span>='TB';<span>break</span><span>; </span><span>case</span> '5':<span>$unit</span>='PB';<span>break</span><span>; </span><span>case</span> '6':<span>$unit</span>='EB';<span>break</span><span>; </span><span>case</span> '7':<span>$unit</span>='ZB';<span>break</span><span>; </span><span>default</span>: <span>$unit</span>='单位未知'<span>; } </span><span>return</span> <span>sprintf</span>('%.2f',<span>$b</span>).<span>$unit</span><span>; } }</span>
Call:
<span>echo</span> formatSize('20667564');
The result is:
19.71MB
Description:
The parameter $b is a number in B, and $times is used to identify how many times this function has been recursed.
For the uncommon units TB, PB, EB, and ZB, please refer to the notes below (sourced from the Internet):
1bit (this bit represents a binary number) 1Byte (this word is also called "bit" when transliterated but represents a hexadecimal number)
1B=1Byte=8bit
1 kB = 1024 B (kB - kilobyte) One MB = 1024 kB (MB - megabyte) One GB = 1024 MB (GB - gigabyte) One TB = 1024 GB (TB - terabyte) One PB = 1024 TB (PB - petabyte) 1 EB = 1024 PB (EB - eksabyte) Ai
1 ZB = 1024 EB (ZB - zettabyte) YB = 1024 ZB (YB - yottabyte) YB = 1024 YB (BB - brontobyte)
yotta, 尧[it], Y. 10^21,
zetta, ze[it], Z. 10^18,
exa, Ai[khazia], E. 10^15,
peta, shoot[it], P. 10 ^12,
tera, 太[拉], T. 10^9,
giga, 吉[卡], G. 10^6,
mega, trillion, M. 10^3
The above introduces the PHP format (file) storage data size (SIZE) display, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











When the HMD Skyline(available on Amazon for $499) was launched last month, it was released in two colors - Neon Pink and Twisted Black. They are now joined by a third color dubbed Blue Topaz. HMD Global has also announced an official case for the ph

Switchcase requires specific code examples to determine variables. In programming, we often need to perform different operations based on different variable values. The switchcase statement is a convenient structure that allows you to select different blocks of code for execution based on the value of a variable. The following is a specific code example that shows how to use the switchcase statement to determine different values of variables: #includeintmain(){

It is very common to use switch statements to select multiple branches in PHP. Usually, a break statement is used to exit the switch statement after each branch. However, there are situations where we do not want to use the break statement. This article will introduce the situation of not using break in the PHP switch statement.

In PHP, break is used to jump out of the current syntax structure and execute the following statements; it can be used in statements such as switch, for, while, and do while. It can terminate the code of the loop body and jump out of the current loop immediately, and execute the following statements after the loop. code. The break statement can take a parameter n, which represents the number of levels to jump out of the loop. If you want to jump out of multiple loops, you can use n to represent the number of levels to jump out of. If there is no parameter, the default is to jump out of the current loop.

Note 1. The function of break is to jump out of the current loop block (for, while, dowhile) or program block (switch). 2. The function of the loop block is to jump out of the loop body currently in the loop. The function of the program block is to interrupt and compare the next case condition. Use break in a switch statement to terminate the switch statement. When break is used in a loop, it breaks out of the loop. There is no point in using break elsewhere. Example intsum=0;inti;for(i=1;i

In the previous article, we took you to learn several loop control structures in JS (while and do-while loops, for loops). Let’s talk about the break and continue statements to jump out of the loop. I hope it will be helpful to everyone!

In the Go language, the break stop statement is used to jump out of the loop in a loop statement and start executing the statement after the loop. The break statement can end the code blocks of for, switch and select. In addition, the break statement can also add a label after the statement to indicate exiting the code block corresponding to a certain label. The label requirement must be defined on the corresponding code block of for, switch and select. .

基本逻辑如下:Stringevent=crsRequest.getEvent();CRSResponsecrsResponse=null;switch(event){caseCRSRequestEvent.APP_START:crsResponse=processAppStartCommand(crsRequest);break;caseCRSRequestEvent.INIT_COMPLETE:crsResponse=processInitCompleteCommand(crsRequest)
