League Mime Type Detection

repository·main·Indexed 23 days ago

https://github.com/thephpleague/mime-type-detection

A PHP package providing a generic interface for detecting MIME types. It features the FinfoMimeTypeDetector for content-based detection using finfo with extension-based fallbacks, and the ExtensionMimeTypeDetector for extension-only detection. The library includes support for custom extension maps via ExtensionToMimeTypeMap and reverse lookups for extensions using the ExtensionLookup interface.

Tokens
1.8K
Snippets
3
Records
13
Agent score
77%

What's inside league/mime-type-detection

  1. Use ExtensionMimeTypeDetector for extension-only detection

    main

    The ExtensionMimeTypeDetector ignores file contents and only determines the mimetype based on the file extension or path.

    $detector = new League\MimeTypeDetection\ExtensionMimeTypeDetector();
    
    // Only detect by extension, ignores the file contents
    $mimeType = $detector->detectMimeType('some/path.php', 'string contents');
    
    // Always returns null
    $mimeType = $detector->detectMimeTypeFromBuffer('string contents');
    
    // Only detect by extension
    $mimeType = $detector->detectMimeTypeFromFile('existing/path.php');
    
    // Only detect by extension
    $mimeType = $detector->detectMimeTypeFromPath('any/path.php');
  2. Use FinfoMimeTypeDetector for content and extension detection

    main

    The FinfoMimeTypeDetector uses finfo to detect mimetypes by file contents and can fall back to extension detection if needed.

    Constructor Options:

    • $pathToMimeDatabase (string): Custom mime database location. Default is ''.
    • $customExtensionMap (array|null): Custom extension fallback map. Default is null.
    • $bufferSampleSize (int): Buffer size limit used to take a sample (substr) from the input buffer to reduce memory consumption.
    $detector = new League\MimeTypeDetection\FinfoMimeTypeDetector();
    
    // Detect by contents, fall back to detection by extension.
    $mimeType = $detector->detectMimeType('some/path.php', 'string contents');
    
    // Detect by contents only, no extension fallback.
    $mimeType = $detector->detectMimeTypeFromBuffer('string contents');
    
    // Detect by actual file, no extension fallback.
    $mimeType = $detector->detectMimeTypeFromFile('existing/path.php');
    
    // Only detect by extension
    $mimeType = $detector->detectMimeTypeFromPath('any/path.php');
  3. Use ExtensionToMimeTypeMap implementations

    main

    These maps are used as fallbacks for finfo based lookup to determine mimetypes from extensions.

    • GeneratedExtensionToMimeTypeMap: A provided implementation generated from the mime-db npm package.
    • OverridingExtensionToMimeTypeMap: A decorator that allows you to override specific mappings from an inner map.
    • EmptyExtensionToMimeTypeMap: An implementation that always returns NULL.
  4. Use ExtensionMimeTypeDetector to detect MIME types from file paths

    main

    The ExtensionMimeTypeDetector class implements the MimeTypeDetector and ExtensionLookup interfaces. It identifies MIME types by inspecting the file extension of a given path using an internal extension-to-MIME-type map.

    By default, it uses a GeneratedExtensionToMimeTypeMap. You can also provide your own instance of ExtensionToMimeTypeMap via the constructor to customize the lookup behavior.

    Key methods:

    • detectMimeType(string $path, $contents): Detects the MIME type based on the file path. The $contents parameter is ignored.
    • detectMimeTypeFromPath(string $path): Specifically performs lookup based on the file extension found in the path.
    • detectMimeTypeFromFile(string $path): Alias for detectMimeTypeFromPath.
    • detectMimeTypeFromBuffer(string $contents): Always returns null as this detector relies on extensions, not file content buffers.
  5. Use FinfoMimeTypeDetector for MIME type detection

    main
    The FinfoMimeTypeDetector uses PHP's finfo extension to detect MIME types. It provides a fallback mechanism: if detection from a buffer or file returns an inconclusive MIME type (like application/octet-stream or text/plain), it attempts to resolve the MIME type using the file extension via an ExtensionToMimeTypeMap.
  6. Configure FinfoMimeTypeDetector

    main

    You can customize the behavior of FinfoMimeTypeDetector via its constructor:

    • string $magicFile: Path to a custom magic file for finfo.
    • ?ExtensionToMimeTypeMap $extensionMap: An instance of ExtensionToMimeTypeMap used for extension-based lookups. If null, a GeneratedExtensionToMimeTypeMap is used.
    • ?int $bufferSampleSize: The number of bytes to sample from a buffer when using detectMimeType. If null, the entire buffer is used.
    • array $inconclusiveMimetypes: A list of MIME types that should be treated as inconclusive, triggering a fallback to extension lookup. By default, this includes application/x-empty, text/plain, text/x-asm, application/octet-stream, and inode/x-empty.
  7. Lookup file extensions using ExtensionMimeTypeDetector

    main

    If the ExtensionMimeTypeDetector is initialized with an extension map that implements the ExtensionLookup interface, you can perform reverse lookups to find extensions associated with a specific MIME type.

    Key methods:

    • lookupExtension(string $mimetype): Returns a single extension string for the given MIME type, or null if no match is found.
    • lookupAllExtensions(string $mimetype): Returns an array of all extensions associated with the given MIME type.
  8. Override extension to MIME type mappings

    main
    If you need to change how a specific file extension is mapped to a MIME type (e.g., forcing .custom to map to application/x-custom instead of the default), use the OverridingExtensionToMimeTypeMap class. This class acts as a decorator for an existing ExtensionToMimeTypeMap. It first checks its own internal $overrides array for the extension; if no override is found, it falls back to the provided $innerMap.