1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
## League Mime Type Detection
[](https://twitter.com/frankdejonge)
[](https://github.com/thephpleague/mime-type-detection)
[](https://github.com/thephpleague/mime-type-detection/releases)
[](https://github.com/thephpleague/mime-type-detection/blob/master/LICENSE)
[](https://travis-ci.com/thephpleague/mime-type-detection)
[](https://scrutinizer-ci.com/g/thephpleague/mime-type-detection/code-structure)
[](https://scrutinizer-ci.com/g/thephpleague/mime-type-detection)
[](https://packagist.org/packages/league/mime-type-detection)

This package supplies a generic mime-type detection interface with a
`finfo` based implementation.
## Usage
```bash
composer require league/mime-type-detection
```
### Detectors
Finfo with extension fallback:
```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.
);
```
Extension only:
```php
$detector = new League\MimeTypeDetection\ExtensionMimeTypeDetector();
// Only detect by extension
$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 mime-type lookup
As a fallback for `finfo` based lookup, an extension map
is used to determine the mime-type. There is an advised implementation
shipped, which is generated from information collected by the npm
package [mime-db](https://www.npmjs.com/package/mime-db).
### Provided extension maps
Generated:
```php
$map = new League\MimeTypeDetection\GeneratedExtensionToMimeTypeMap();
// string mime-type or NULL
$mimeType = $map->lookupMimeType('png');
```
Overriding decorator
```php
$innerMap = new League\MimeTypeDetection\GeneratedExtensionToMimeTypeMap();
$map = new League\MimeTypeDetection\OverridingExtensionToMimeTypeMap($innerMap, ['png' => 'custom/mimetype']);
// string "custom/mimetype"
$mimeType = $map->lookupMimeType('png');
```
Empty:
```php
$map = new League\MimeTypeDetection\EmptyExtensionToMimeTypeMap();
// Always returns NULL
$mimeType = $map->lookupMimeType('png');
```
|