Ale*_*lex 121 php file filesize
如何filesize()使用MegaBytes,KiloBytes等将PHP 函数的输出转换为漂亮的格式?
喜欢:
Adn*_*nan 258
这是一个示例:
<?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;
}
?>
Run Code Online (Sandbox Code Playgroud)
PiT*_*ber 61
更好的是我从我发现的插件创建的这个版本:
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];
}
Run Code Online (Sandbox Code Playgroud)
来自filesize()doc的注释
由于PHP的整数类型已签名且许多平台使用32位整数,因此某些文件系统函数可能会对大于2GB的文件返回意外结果
Ali*_*xel 42
更清洁的方法:
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;
}
Run Code Online (Sandbox Code Playgroud)
Tef*_*ffi 14
我认为这是一种更好的方法.简单直接.
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] );
}
Run Code Online (Sandbox Code Playgroud)
该问题的所有答案都使用 1 KB = 1024 字节,这是错误的!(1 kibibyte = 1024 字节)
由于问题要求转换文件大小,因此应使用1 KB = 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];
}
Run Code Online (Sandbox Code Playgroud)
这是基于@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';
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
这将是一个更清洁的实现:
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
180521 次 |
| 最近记录: |