dflydev/apache-mime-types
Parse and query Apache mime.types mappings in PHP. Includes bundled Apache mime.types plus JSON representation. Use parser to read mime.types files, or repositories (PHP, JSON, flat) to look up MIME type by extension and extensions by type.
mime.types), it introduces no external dependencies or runtime overhead, aligning well with Laravel’s modular architecture.Storage, Request validation) and packages like spatie/laravel-medialibrary or intervention/image.Parser, PhpRepository, JsonRepository) is simple, reducing integration complexity.mime.types is static; updates require manual version bumps).mime.types files at runtime (unlikely for most use cases).mime-type package (if used) or augment it (e.g., for Apache-specific types)?JsonRepository preferred over PhpRepository for faster lookups (JSON parsing is a one-time cost)?mime.types updates?symfony/mime (bundled with Symfony) or league/mime-type-detection for broader functionality.Illuminate\Http\Request for file()->getClientMimeType() or file()->getClientOriginalExtension().Accept headers in API routes (e.g., Route::where('accept', 'text/.*')).spatie/laravel-medialibrary for MIME-based metadata.fileinfo extension (used by Laravel’s mime-type helper), but this package offers Apache-specific types.PhpRepository or JsonRepository.// Before: Hardcoded
$extensions = ['html', 'htm'];
// After: Dynamic
$extensions = app(Dflydev\ApacheMimeTypes\PhpRepository::class)->findExtensions('text/html');
application/octet-stream defaults, custom types) with Laravel’s phpunit tests.// config/app.php
'bindings' => [
Dflydev\ApacheMimeTypes\PhpRepository::class => fn() => new Dflydev\ApacheMimeTypes\PhpRepository(),
];
mime.types path if needed (e.g., for custom Apache configs).Request validation rules).storing in HasFile models).Content-Type headers) or frontend assets (e.g., Accept headers).mime.types changes (rare).try {
$type = $repository->findType('unknown-extension');
} catch (\InvalidArgumentException $e) {
Log::debug('Unknown MIME type', ['extension' => 'unknown-extension']);
}
.JPG vs .jpg).$repository->getAllTypes(); // Inspect full mapping
$repository->getAllExtensions(); // Verify coverage
PhpRepository vs JsonRepository.$extensions = Cache::remember("mime_extensions_{$type}", now()->addHours(1), fn() =>
$repository->findExtensions($type)
);
mime.types file (mitigate by validating the file path and format).application/octet-stream).InvalidArgumentException for unknown types/extensions (catch and log as above).mime.types path (validate path existence).Parser).// In a Form Request
public function rules()
{
return [
'file' => ['required', 'mimetypes:text/html,text/plain'], // Custom rule using the package
];
}
How can I help you explore Laravel packages today?