PHP File Size Format Function collection _php instance

Source: Internet
Author: User
Tags pow

For example, encountered a very large file has 49957289167B, we look at a long string of numbers after the unit is byte B, or do not know the size of this file is a concept, we convert it to GB as a unit, is 46.53GB. Use the following functions to do this work:

Copy Code code 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 (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, ' 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 format
* @param integer $size initial file size, in bytes
* @return Array formatted file size and unit arrays 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 format
* @param $bytes The actual size of the file, Unit byte
* @param $prec conversion accuracy, the default precision to two decimal places
* @return converted size + unit 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;
}

/**
* The capacity of the format
* @param String file name (file path)
* @return If file exists returns a formatted string if error returns error message Unknown file
*/
function Sizeformat ($fileName) {
Get the size of a file
@ $filesize =filesize ($fileName);
If the file does not exist return an 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 >= 1024) $filesize = round ($filesize/1024 * 100)/100. ' KB ';
else $filesize = $filesize. ' Bytes ';
return $filesize;
}
Test function
echo Sizeformat (' config.inc.php ');

/**
* File Size format
* @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 function of the most important is based on the number of bytes in the file, Judge should select the unit of statistics, that is, a file in a certain unit such as MB, then the file must be less than 1GB, or, of course, to use GB as a unit, and the file is greater than KB, or the smaller 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 choose the number of decimal places to be rounded.
function ROUND_DP ($num, $DP)
{
$sh = POW ($DP);
Return (Round ($num * $sh)/$sh);
}

So we can be very good statistics of the size of any one file, first through the system with the FileSize () function to get the file bytes, and then use these functions to convert to the appropriate units on the

Related Article

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.