Files
Fabián Gonzalo Artur de la Villarmois bae79d68ab * Fix CI/CD php version
* Add Pint
* Format code
2026-03-29 17:49:51 +13:00

29 lines
650 B
PHP

<?php
namespace App\Helpers;
class FileSize
{
private const UNITS = ['', 'K', 'M', 'G', 'T'];
private const THRESHOLD = 1024;
/**
* Format bytes into human-readable file size.
*/
public static function format(int $bytes): string
{
if ($bytes === 0) {
return '0';
}
$unitIndex = (int) floor(log($bytes, self::THRESHOLD));
$unitIndex = min($unitIndex, count(self::UNITS) - 1);
$size = $bytes / pow(self::THRESHOLD, $unitIndex);
$decimals = $unitIndex === 0 ? 0 : 2;
return number_format($size, $decimals, '.', '').self::UNITS[$unitIndex];
}
}