PHP file size Formatting Function set

Source: Internet
Author: User

For example, if you encounter a large file named 49957289167B, you can see that the unit behind such a long string of numbers is byte B, but you still don't know the concept of the file size, we convert it to GB, Which is 46.53 GB. You can use the following functions to complete this task:
Copy codeThe 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.