PHP 文件大小 MB/KB 转换 [英] PHP filesize MB/KB conversion
本文介绍了PHP 文件大小 MB/KB 转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将 PHP 的 filesize()
函数的输出转换为具有 MegaBytes、KiloBytes 等的良好格式?
How can I convert the output of PHP's filesize()
function to a nice format with MegaBytes, KiloBytes etc?
喜欢:
- 如果大小小于 1 MB,则以 KB 为单位显示大小
- 如果它在 1 MB - 1 GB 之间,则以 MB 为单位显示
- 如果它更大 - 以 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;
}
?>
这篇关于PHP 文件大小 MB/KB 转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文