PHP File size Format Function collection

Source: Internet
Author: User

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:

The 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 ' and ' 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 arrays formatted in Byte, KB, MB, GB, 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);

/**
* File size unit formatting
* @param $bytes file actual size, unit byte
* @param $prec post-conversion accuracy, default to two decimal places
* @return converted size + unit of string
*/
function Fsizeformat ($bytes, $prec =2) {
$rank = 0;
$size = $bytes;
$unit = "B";
while ($size >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 return formatted string if error returned error message Unknown file
*/
function Sizeformat ($fileName) {
Get the size of a file
@ $filesize =filesize ($fileName);
Return error message if file does not exist
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 sizes
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 trade off 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

PHP File size Format Function collection

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.