<?php declare(strict_types=1); namespace mystic\forum\utils; final class FileUtils { use StaticClass; private const MIME_FILE = __DIR__ . "/../../../assets/mimetypes"; public const DEFAULT_MIME_TYPE = "application/octet-stream"; private static ?array $extToMime = null; private static function ensureMimeMapping(): void { if (self::$extToMime !== null) return; self::$extToMime = []; $hFile = fopen(self::MIME_FILE, "r"); while (($ln = fgets($hFile)) !== false) { [$mime, $extensions] = preg_split('/\t+/', $ln, 2); $extensions = explode(" ", strtolower($extensions)); foreach ($extensions as $ext) self::$extToMime[$ext] = $mime; } } public static function getMimeTypeForExtension(string $extension, string $defaultMimeType = self::DEFAULT_MIME_TYPE): string { self::ensureMimeMapping(); return self::$extToMime[strtolower($extension)] ?? $defaultMimeType; } }