如何将PHP的filesize()
函数的输出转换为带有MB、KB等格式的好格式?
例如:
- 如果大小小于1MB,则以KB为单位显示大小。
- 如果大小在1MB到1GB之间,则以MB为单位显示。
- 如果大小大于1GB,则以GB为单位显示。
这里有一个示例:
<?php
// Snippet from PHP Share: http://www.phpshare.org
function formatSizeUnits($bytes)
{
if ($bytes >= 1073741824)
{
$bytes = number_format($bytes / 1073741824, 2) . ' GB';
}
elseif ($bytes >= 1048576)
{
$bytes = number_format($bytes / 1048576, 2) . ' MB';
}
elseif ($bytes >= 1024)
{
$bytes = number_format($bytes / 1024, 2) . ' KB';
}
elseif ($bytes > 1)
{
$bytes = $bytes . ' bytes';
}
elseif ($bytes == 1)
{
$bytes = $bytes . ' byte';
}
else
{
$bytes = '0 bytes';
}
return $bytes;
}
?>
number_format()
这个细节很不错。实际上,看起来WordPress本身也使用了你这个简单函数的变体:https://developer.wordpress.org/reference/functions/size_format/ - undefined更好的是,我从一个插件创建了这个版本:
function filesize_formatted($path)
{
$size = filesize($path);
$units = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$power = $size > 0 ? floor(log($size, 1024)) : 0;
return number_format($size / pow(1024, $power), 2, '.', ',') . ' ' . $units[$power];
}
来自filesize() 文档的说明:
由于PHP的整数类型是带符号的,并且许多平台使用32位整数,因此对于文件大小超过2GB的文件,一些文件系统函数可能会返回意外的结果。
filesize()
函数文档 中的注意事项: “由于PHP的整数类型是带符号的,许多平台使用32位整数,一些文件系统函数对于超过2GB的文件可能返回意外结果”。因为这个原因和其他很多原因,我不相信会有人使用PHP来操作以TB
或更高单位大小计算的文件。此外,我怀疑你的函数比Alix Axel的函数更快,因为你使用的pow
函数在资源消耗上与Alix使用的log
类似。 - trejder更加简洁的方法:
function Size($path)
{
$bytes = sprintf('%u', filesize($path));
if ($bytes > 0)
{
$unit = intval(log($bytes, 1024));
$units = array('B', 'KB', 'MB', 'GB');
if (array_key_exists($unit, $units) === true)
{
return sprintf('%d %s', $bytes / pow(1024, $unit), $units[$unit]);
}
}
return $bytes;
}
if
块,即从单个(最后一个)else
开始,然后通过所有的 elseif
上升,直到最后到达第一个(按顺序)的 if
吗?只是好奇,你为什么会得到这样的结果? - trejderif
语句。对于1..1023的值,它比任何大于该值的给定值都要慢得多。我认为这与PHP的log
实现有关(毕竟,我使用的是1024进制)。 - Alix Axel我认为这种方法更好。简单而直接。
public function sizeFilter( $bytes )
{
$label = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB' );
for( $i = 0; $bytes >= 1024 && $i < ( count( $label ) -1 ); $bytes /= 1024, $i++ );
return( round( $bytes, 2 ) . " " . $label[$i] );
}
这是基于@adnan的很棒的答案。
更改:
您仍然可以将filesize()调用从函数中提取出来,以获得一个纯字节格式化函数。但这适用于文件。
/**
* Formats filesize in human readable way.
*
* @param file $file
* @return string Formatted Filesize, e.g. "113.24 MB".
*/
function filesize_formatted($file)
{
$bytes = filesize($file);
if ($bytes >= 1073741824) {
return number_format($bytes / 1073741824, 2) . ' GB';
} elseif ($bytes >= 1048576) {
return number_format($bytes / 1048576, 2) . ' MB';
} elseif ($bytes >= 1024) {
return number_format($bytes / 1024, 2) . ' KB';
} elseif ($bytes > 1) {
return $bytes . ' bytes';
} elseif ($bytes == 1) {
return '1 byte';
} else {
return '0 bytes';
}
}
所有回答该问题的都使用了1千字节=1024字节,这是错误的!(1 Kibibyte = 1024字节)
由于该问题要求转换文件大小,因此应该使用1千字节=1000字节(参见https://wiki.ubuntu.com/UnitsPolicy)
function format_bytes($bytes, $precision = 2) {
$units = array('B', 'KB', 'MB', 'GB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1000));
$pow = min($pow, count($units) - 1);
$bytes /= pow(1000, $pow);
return round($bytes, $precision) . ' ' . $units[$pow];
}
function size2Byte($size) {
$units = array('KB', 'MB', 'GB', 'TB');
$currUnit = '';
while (count($units) > 0 && $size > 1024) {
$currUnit = array_shift($units);
$size /= 1024;
}
return ($size | 0) . $currUnit;
}
<?php
$units = explode(' ','B KB MB GB TB PB');
echo("<html><body>");
echo('file size: ' . format_size(filesize("example.txt")));
echo("</body></html>");
function format_size($size) {
$mod = 1024;
for ($i = 0; $size > $mod; $i++) {
$size /= $mod;
}
$endIndex = strpos($size, ".")+3;
return substr( $size, 0, $endIndex).' '.$units[$i];
}
?>
function calcSize($size,$accuracy=2) {
$units = array('b','Kb','Mb','Gb');
foreach($units as $n=>$u) {
$div = pow(1024,$n);
if($size > $div) $output = number_format($size/$div,$accuracy).$u;
}
return $output;
}
//Get the size in bytes
function calculateFileSize($size)
{
$sizes = ['B', 'KB', 'MB', 'GB'];
$count=0;
if ($size < 1024) {
return $size . " " . $sizes[$count];
} else{
while ($size>1024){
$size=round($size/1024,2);
$count++;
}
return $size . " " . $sizes[$count];
}
}
size_format()
。 - Earlee