# League Mime Type Detection for PHP File Inspection

> League Mime Type Detection gives PHP applications a shared interface for MIME type detection from file contents, file paths, or extensions. It combines PHP's `finfo` support with generated extension mapping while exposing alternative detectors and extension lookup contracts.

**thephpleague/mime-type-detection** — League Mime Type Detection

- Repository: https://github.com/thephpleague/mime-type-detection
- Website: https://flysystem.thephpleague.com/
- Stars: 1,344 · Forks: 29
- Language: PHP
- License: MIT
- Published: 2026-10-16 · Updated: 2026-10-16 · Language: en
- Canonical page: https://hysenlabs.com/projects/thephpleague-mime-type-detection

## Why League Mime Type Detection Exists

League Mime Type Detection addresses a narrow but common PHP integration problem: code that needs a MIME type can inspect file contents, infer a type from a filename extension, or combine both approaches, but direct calls to `finfo` spread detection details across an application. The package supplies a generic interface backed by a `finfo` implementation and a separate extension-only implementation. This is useful to library authors, framework integrations, upload-processing code, and file-handling applications that want detection behavior to remain behind contracts rather than direct calls to a particular PHP mechanism.

The documented consumer contracts are `League\MimeTypeDetection\MimeTypeDetector`, which detects MIME types from filenames and file contents, and `League\MimeTypeDetection\ExtensionLookup`, which looks up one or all extensions associated with a MIME type. The README says application code is advised to couple to these interfaces. That recommendation matters because it can let detection implementation details change without requiring every caller to know whether a result came from inspected bytes or a filename extension.

The package does not promise that a filename or buffer is safe, valid, or acceptable for a particular service. It reports a MIME type. Deciding whether that result is appropriate for an upload, matching an expected media type, or rejecting suspicious content remains the caller's responsibility. The README documents detection and lookup APIs, but it does not define an allowlist policy, file scanner, or content-validation workflow.

## Finfo Detection and Extension Fallback

`FinfoMimeTypeDetector` is the primary documented detector. Its normal `detectMimeType` path takes both a path-like string and a contents buffer, attempts detection from the contents, and falls back to extension mapping. This design suits applications that have some file bytes available but still need to handle cases where content inspection cannot determine a useful type.

Separate methods let callers choose a narrower policy. `detectMimeTypeFromBuffer` examines contents without extension fallback, while `detectMimeTypeFromFile` reads an actual file and does not apply extension fallback. `detectMimeTypeFromPath` uses the extension alone. The separation is the package's most useful trade-off: a caller can opt into content-first behavior with fallback or demand one detection method explicitly.

The constructor also accepts three optional settings. A custom MIME database path can replace the default location. A custom extension map can replace or extend the fallback behavior passed to the detector. A buffer sample size limits how much of the supplied input is sampled with `substr`, reducing memory consumption for large buffers. The README does not specify the default sample size, custom map format, database compatibility requirements, or the exact threshold at which `finfo` declines a result, so those points must be checked in the implementation before relying on them for a strict application policy.

## Generated and Replaceable Extension Maps

When `FinfoMimeTypeDetector` cannot derive a MIME type from contents, it uses an extension map. The package ships `GeneratedExtensionToMimeTypeMap`, described as generated from information collected by the npm package `mime-db`. The map exposes `lookupMimeType('png')` and returns a MIME type string or `null`. This fallback remains an inference based on a suffix, so it should not be confused with confirmation that the bytes are PNG data.

Two decorators make the mapping layer replaceable. `OverridingExtensionToMimeTypeMap` receives an inner map and an override array. The README demonstrates assigning `'png' => 'custom/mimetype'`, after which the decorator returns that exact string. This supports application-specific corrections without changing the generated map itself. `EmptyExtensionToMimeTypeMap` always returns `null`, which is useful when extension fallback should be disabled deliberately.

The reverse direction is available through `lookupExtension` and `lookupAllExtensions`. The first returns a string or `null`, while the second returns an array of extensions. This capability was added in version `1.13.0`. Its practical value depends on the selected detector's mapping data: the README does not promise a one-to-one relationship between a MIME type and an extension, so consumers should not assume that the first returned extension is a canonical or required filename ending.

## Installing League Mime Type Detection in PHP

The README identifies Composer as the installation route and lists PHP 7.2 or later in its compatibility badge. Install the package from a PHP project that already uses Composer:

```bash
composer require league/mime-type-detection
```

After Composer completes, application code can instantiate `FinfoMimeTypeDetector`. This first call detects from the supplied contents and falls back to the `php` extension:

```php
$detector = new League\MimeTypeDetection\FinfoMimeTypeDetector();
$mimeType = $detector->detectMimeType('some/path.php', 'string contents');
```

The expected interface is a MIME type string when detection succeeds, with `null` possible where the documented APIs permit it. The repository contains `composer.json`, `src/`, `test_files/`, and PHPUnit-related files. The README does not document a standalone runtime service, configuration file, or required environment variable, so installation does not introduce a separate server or background process.

For a first integration, keep the detector behind `MimeTypeDetector` rather than constructing it throughout the codebase. Applications that consume extension information can also depend on `ExtensionLookup`. The README calls these contracts advisory, not mandatory, but using them reduces coupling to `FinfoMimeTypeDetector` and the extension map classes.

## Content-Only and Extension-Only Policies

The package makes it possible to reject an unsound fallback rather than accepting it implicitly. An upload handler that requires evidence from the bytes can call `detectMimeTypeFromBuffer`. That method does not fall back to the extension, so a caller can avoid accepting a type merely because the filename ends in a recognized suffix. The same restriction applies to `detectMimeTypeFromFile`.

`ExtensionMimeTypeDetector` provides the opposite policy. Its `detectMimeType` and `detectMimeTypeFromPath` methods look only at extensions, while its `detectMimeTypeFromBuffer` always returns `null` because it ignores contents. This is a concrete alternative for systems whose input is deliberately limited to filename metadata, such as a catalog that stores an extension as its declared format. It is the wrong choice when file contents must determine the result.

Neither detector supplies a content allowlist. The distinction is about the source of inference, not about whether a detected MIME type is trusted. The README does not document magic-byte validation, malware scanning, archive inspection, or reconciliation between a claimed type and an actual type. Those controls would need separate project-specific logic.

## Maintenance, Releases, and MIT Implications

The repository is not archived, and its last push was on 2026-07-09. That is a recent recorded change, but recent repository activity does not establish API stability by itself. No GitHub releases are listed. Upgrade planning therefore cannot rely on published GitHub release notes, and the package's version-specific change history would need to be checked through Composer and the repository's `CHANGELOG.md` before changing a locked dependency.

Upgrade cost depends on which contracts and concrete classes an application uses. Code limited to `MimeTypeDetector` has a smaller source-level coupling than code that directly constructs `GeneratedExtensionToMimeTypeMap`, `OverridingExtensionToMimeTypeMap`, or `EmptyExtensionToMimeTypeMap`. Custom MIME database paths, extension overrides, and buffer sample sizes also add configuration that must be reviewed during an upgrade.

The repository identifies the project as licensed under MIT. That generally indicates an open source licensing model and is relevant to adoption, but the MIT text and local legal requirements govern actual use. The README does not discuss warranty terms or application-specific compliance, so no broader legal conclusion can be drawn from the license label alone.

## Conclusion

Adopt this package when PHP code needs a replaceable MIME type detection interface with content-first inspection and extension fallback. Do not choose it when fixed MIME type enforcement is required. Check first that `finfo` is available, confirm whether the generated map reflects the media types your application handles, and verify the repository's `composer.json` before upgrading.

## FAQ

### How do I install League Mime Type Detection in a PHP project?

The README specifies Composer installation with `composer require league/mime-type-detection`. Its compatibility badge lists PHP 7.2 or later.

### How does FinfoMimeTypeDetector handle a file whose contents do not reveal its type?

Its general `detectMimeType` method attempts detection from the supplied contents and falls back to extension mapping. Call `detectMimeTypeFromBuffer` or `detectMimeTypeFromFile` when extension fallback must be disabled.

### Can League Mime Type Detection return file extensions for a MIME type?

Yes. Implementations can use `lookupExtension` for one extension or `lookupAllExtensions` for an array. This lookup capability was added in version `1.13.0`.

## Sources

- [Issues](https://github.com/thephpleague/mime-type-detection/issues)
- [License: MIT](https://github.com/thephpleague/mime-type-detection/blob/main/LICENSE)
- [Project website](https://flysystem.thephpleague.com/)
- [README](https://github.com/thephpleague/mime-type-detection/blob/main/README.md)
- [thephpleague/mime-type-detection on GitHub](https://github.com/thephpleague/mime-type-detection)

---

Hysen Labs editorial analysis, written from the project's own repository and release notes. Cite the canonical page: https://hysenlabs.com/projects/thephpleague-mime-type-detection
