PHP File Size Format Function Collection _php Tutorial

Source: Internet
Author: User
Tags pow
For example, encounter a very large file 49957289167B, we see a long string of numbers behind the unit is byte B, or do not know the size of this file is a concept, we convert it to GB, that is 46.53GB. The following functions are used to accomplish this task:
Copy CodeThe code is as follows:
Convert units
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 (1024x768, ($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 ' = +, ' octets ' = 1);
foreach ($size as $k = + $v)
if ($o >= $v) {
if ($k = = ' octets ')
return round ($o). ' '. $k;
Return Number_format ($o/$v, 2, ', ', '). ' '. $k;
}
}
/**
* File size formatting
* @param integer $size initial file size in byte
* @return array of file size and unit array after format, in bytes, KB, M B, GB, TB
*/
Function File_size_format ($size = 0, $dec = 2) {
$unit = array ("B", "KB", "MB", "GB", "TB", "PB"); $pos = 0;
while ($size >=) {
$size/=;
$pos + +;
}
$result [' size '] = Round ($size, $DEC);
$result [' unit '] = $unit [$pos];
return $result [' size ']. $result [' Unit '];
}
Echo File_size_format (123456789);

/**
* File size units formatted
* @param $bytes file actual size, in bytes
* @param $prec converted precision, default to two bits after decimal point
* @return converted size + unit of string
*/
Function Fsizeformat ($bytes, $prec =2) {
$rank =0;
$size = $bytes;
$unit = "B";
while ($size > (1024x768) {
$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 a file exists that returns a formatted string if the error returns an error message Unknown file
*/
Func tion Sizeformat ($fileName) {
//Get file size
@ $filesize =filesize ($fileName);
//If the file does not exist return error message
if (false== $filesize) {
return ' Unknown file ';
}
//format 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 >= 1024x768) $filesize = round ($filesize/1024 * 100)/100. ' KB ';
Else $filesize = $filesize. ' Bytes ';
return $filesize;
}
//Test 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 main point of the function is based on the number of bytes of the file, determine the statistical units should be selected, that is, a file with a certain unit such as MB, then the file is certainly less than 1GB, or, of course, GB as a unit, and the file to be greater than KB, otherwise, smaller units are counted. The function code is as follows

//size () Statistics file size
function size ($byte)
{
if ($byte < 1024x768) {
$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;
} The
//helper function round_up (), which is used to choose the number of decimal places, rounded.
Function ROUND_DP ($num, $DP)
{
$sh = POW ($DP);
Return (Round ($num * $sh)/$sh);
}

So that we can do a good job of calculating the size of any file, first by the system's own filesize () function to obtain the number of bytes of the file, and then use the above functions to convert to the appropriate units can be

http://www.bkjia.com/PHPjc/739778.html www.bkjia.com true http://www.bkjia.com/PHPjc/739778.html techarticle For example, encounter a very large file 49957289167B, we see a long string of numbers behind the unit is byte B, or do not know the size of the file is a what concept, we put it ...

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