파일 및 디스크 용량 구할 때(KB, MB, GB)
테라 바이트는 아래 추가 되어 있지 않네요.
대문자 B는 바이트를 의미합니다.
소문자 b는 비트를 의미 합니다. 일부 잘 모르는 분은 아무렇게나 사용하기도 합니다. 철자를 다 넣어주면 문제 되진 않습니다.
약자로 1글자만 사용할때는 반드시 지켜줘야 합니다.
테라바이트는 1024를 추가로 곱해서 넣어 주면 됩니다. 컴퓨터는 2진수를 사용하기 때문에 2의 승수로 처리하게 됩니다.
class FileSizeFormatter
{
static public 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;
}
}
$fileSize = 37834304591; // 예시 파일 크기
$convertedSize = FileSizeFormatter::formatSizeUnits($fileSize);
echo $convertedSize;
exit;