Php file size formatting function set

Source: Internet
Author: User
In php, there is a function that comes with the system to calculate the file size, that is, filesize (), but this function is in bytes. in some cases, we need to intuitively understand the size of a file, not only the unit of Byte B, but also the size of KB, MB, GB, or even larger. for example, if a large file contains 49957289167B, we can see that the unit behind such a long string of numbers is byte B. We still don't know what the size of this file is. we convert it to GB, which is 46.53 GB. You can use the following functions to complete this task:
The code is as follows:
// Conversion Unit
Function setupSize ($ fileSize ){
$ Size = sprintf ("% u", $ fileSize );
If ($ size = 0 ){
Return ("0 Bytes ");
}
$ Sizename = array ("Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB ", "YB ");
Return round ($ size/pow (1024, ($ I = floor (log ($ size, 1024), 2). $ sizename [$ I];
}
Function byte_format ($ size, $ dec = 2 ){
$ A = array ("B", "KB", "MB", "GB", "TB", "PB ");
$ Pos = 0;
While ($ size> = 1024 ){
$ Size/= 1024;
$ Pos ++;
}
Return round ($ size, $ dec). "". $ a [$ pos];
}
/* Use: echo format_size (filesize ("fichier "));
Example result: 13, 37 Ko */

Function format_size ($ o ){
$ Size = array ('go' => 1073741824, 'Mo' => 1048576, 'ko' => 1024, 'ots' => 1 );
Foreach ($ size as $ k => $ v)
If ($ o >=$ v ){
If ($ k = 'ots ')
Return round ($ o). ''. $ k;
Return number_format ($ o/$ v, 2, ',', ''). '. $ k;
}
}
/**
* File size formatting
* @ Param integer $ size initial file size, in bytes
* @ Return array the size and unit array of the formatted file, measured in byte, KB, MB, GB, and TB.
*/
Function file_size_format ($ size = 0, $ dec = 2 ){
$ Unit = array ("B", "KB", "MB", "GB", "TB", "PB ");
$ Pos = 0;
While ($ size> = 1024 ){
$ Size/= 1024;
$ Pos ++;
}
$ Result ['size'] = round ($ size, $ dec );
$ Result ['unit '] = $ unit [$ pos];
Return $ result ['size']. $ result ['unit '];
}
Echo file_size_format (123456789 );

/**
* Format the file size unit.
* @ Param $ actual bytes file size, in bytes
* @ Param $ prec accuracy after conversion. by default, it is accurate to the last two digits of the decimal point.
* @ Return the converted size + unit string
*/
Function fsizeformat ($ bytes, $ prec = 2 ){
$ Rank = 0;
$ Size = $ bytes;
$ Unit = "B ";
While ($ size & gt; 1024 ){
$ Size = $ size/1024;
$ Rank ++;
}
$ Size = round ($ size, $ prec );
Switch ($ rank ){
Case "1 ":
$ Unit = "KB ";
Break;
Case "2 ":
$ Unit = "MB ";
Break;
Case "3 ":
$ Unit = "GB ";
Break;
Case "4 ":
$ Unit = "TB ";
Break;
Default:

}
Return $ size. "". $ unit;
}

/**
* Capacity formatting
* @ Param String file name (File path)
* @ Return if the File exists, the formatted string is returned. if the error is returned, the error message Unknown File is returned.
*/
Function sizeFormat ($ fileName ){
// Obtain the file size
@ $ Filesize = filesize ($ fileName );
// If the file does not exist, an error message is returned.
If (false = $ filesize ){
Return 'unknown file ';
}
// Format the file capacity information
If ($ filesize> = 1073741824) $ filesize = round ($ filesize/1073741824*100)/100. 'GB ';
Elseif ($ filesize> = 1048576) $ filesize = round ($ filesize/1048576*100)/100. 'mb ';
Elseif ($ filesize> = 1024) $ filesize = round ($ filesize/1024*100)/100. 'KB ';
Else $ filesize = $ filesize. 'bytes ';
Return $ filesize;
}
// Test the function
Echo sizeFormat ('config. inc. php ');

/**
* File size formatting
* @ Param type $ filesize
*/
Private function sizeCount ($ filesize)
{
If ($ filesize> = 1073741824 ){
$ Filesize = round ($ filesize/1073741824*100)/100. 'GB ';
}

Else if ($ filesize> = 1048576 ){
$ Filesize = round ($ filesize/1048576*100)/100. 'MB ';
}

Else if ($ filesize> = 1024 ){
$ Filesize = round ($ filesize/1024*100)/100. 'KB ';
}

Return $ filesize;
}


// The most important function is to determine the statistical unit to be selected based on the number of bytes of the file. that is to say, if a file uses a certain unit such as MB, the file must be smaller than 1 GB, otherwise, use GB as the unit, and the file must be larger than KB. Otherwise, use a smaller unit for statistics. The function code is as follows:

// Size () statistics file size
Function size ($ byte)
{
If ($ byte <1024 ){
$ Unit = "B ";
}
Else if ($ byte <10240 ){
$ Byte = round_dp ($ byte/1024, 2 );
$ Unit = "KB ";
}
Else if ($ byte <102400 ){
$ Byte = round_dp ($ byte/1024, 2 );
$ Unit = "KB ";
}
Else if ($ byte <1048576 ){
$ Byte = round_dp ($ byte/1024, 2 );
$ Unit = "KB ";
}
Else if ($ byte <10485760 ){
$ Byte = round_dp ($ byte/1048576, 2 );
$ Unit = "MB ";
}
Else if ($ byte <104857600 ){
$ Byte = round_dp ($ byte/1048576,2 );
$ Unit = "MB ";
}
Else if ($ byte <1073741824 ){
$ Byte = round_dp ($ byte/1048576, 2 );
$ Unit = "MB ";
}
Else {
$ Byte = round_dp ($ byte/1073741824, 2 );
$ Unit = "GB ";
}

$ Byte. = $ unit;
Return $ byte;
}
// Auxiliary function round_up (), which is used to select the number of decimal places and rounded down.
Function round_dp ($ num, $ dp)
{
$ Sh = pow (10, $ dp );
Return (round ($ num * $ sh)/$ sh );
}

In this way, we can calculate the size of any file. First, we can use the system's built-in filesize () function to obtain the number of bytes of the file, use the above functions to convert them into appropriate units.

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.