### Install League Mime Type Detection Source: https://github.com/thephpleague/mime-type-detection/blob/main/readme.md Installs the League Mime Type Detection package using Composer. ```bash composer require league/mime-type-detection ``` -------------------------------- ### FinfoMimeTypeDetector Usage Source: https://github.com/thephpleague/mime-type-detection/blob/main/readme.md Demonstrates how to use the FinfoMimeTypeDetector for MIME type detection, including detection by content, file, and extension fallback. Also shows constructor options for custom mime databases, extension maps, and buffer sample sizes. ```php $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'); // Constructor options $detector = new League\MimeTypeDetection\FinfoMimeTypeDetector( $pathToMimeDatabase, // Custom mime database location, default: '' $customExtensionMap, // Custom extension fallback mapp, default: null $bufferSampleSize // Buffer size limit, used to take a sample (substr) from the input buffer to reduce memory consumption. ); ``` -------------------------------- ### EmptyExtensionToMimeTypeMap Source: https://github.com/thephpleague/mime-type-detection/blob/main/readme.md Demonstrates the EmptyExtensionToMimeTypeMap, which provides no mappings and will always return NULL when looking up MIME types by extension. ```php $map = new League\MimeTypeDetection\EmptyExtensionToMimeTypeMap(); // Always returns NULL $mimeType = $map->lookupMimeType('png'); ``` -------------------------------- ### ExtensionMimeTypeDetector Usage Source: https://github.com/thephpleague/mime-type-detection/blob/main/readme.md Demonstrates the ExtensionMimeTypeDetector, which exclusively uses file extensions for MIME type detection and ignores file content. It also shows how to detect MIME types from file paths. ```php $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'); ``` -------------------------------- ### Extension Lookup by MIME Type Source: https://github.com/thephpleague/mime-type-detection/blob/main/readme.md Illustrates how to look up file extensions associated with a given MIME type using the ExtensionLookup interface. It shows how to retrieve a single extension or all associated extensions. ```php // string | null $extension = $detector->lookupExtension($mime$type); // array $allExtensions = $detector->lookupAllExtensions($mimeType); ``` -------------------------------- ### OverridingExtensionToMimeTypeMap Source: https://github.com/thephpleague/mime-type-detection/blob/main/readme.md Shows how to use the OverridingExtensionToMimeTypeMap to customize the extension-to-MIME type mapping. This decorator allows overriding existing mappings or adding new ones. ```php $innerMap = new League\MimeTypeDetection\GeneratedExtensionToMimeTypeMap(); $map = new League\MimeTypeDetection\OverridingExtensionToMimeTypeMap($innerMap, ['png' => 'custom/mimetype']); // string "custom/mimetype" $mimeType = $map->lookupMimeType('png'); ``` -------------------------------- ### GeneratedExtensionToMimeTypeMap Source: https://github.com/thephpleague/mime-type-detection/blob/main/readme.md Demonstrates the use of GeneratedExtensionToMimeTypeMap for looking up MIME types based on file extensions. This map is generated from the npm package 'mime-db'. ```php $map = new League\MimeTypeDetection\GeneratedExtensionToMimeTypeMap(); // string mime-type or NULL $mimeType = $map->lookupMimeType('png'); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.