|
1 year ago | |
---|---|---|
.. | ||
.github | 1 year ago | |
bin | 1 year ago | |
src | 1 year ago | |
test_files | 1 year ago | |
.gitattributes | 1 year ago | |
.gitignore | 1 year ago | |
.php-cs-fixer.dist.php | 1 year ago | |
.scrutinizer.yml | 1 year ago | |
CHANGELOG.md | 1 year ago | |
LICENSE | 1 year ago | |
composer.json | 1 year ago | |
phpstan.neon | 1 year ago | |
phpunit.php | 1 year ago | |
phpunit.xml.dist | 1 year ago | |
readme.md | 1 year ago |
This package supplies a generic mime-type detection interface with a
finfo
based implementation.
composer require league/mime-type-detection
Your code is advised to couple to the following interfaces:
League\MimetypeDetection\MimeTypeDetector
League\MimetypeDetection\ExtensionLookup
1.13.0
.Finfo with extension fallback:
$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:
$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');
This feature was added in version
1.13.0
The various implementations can look up the extensions that can be used for a given mime-type.
// string | null
$extension = $detector->lookupExtension($mime$type);
// array<string>
$allExtensions = $detector->lookupAllExtensions($mimeType);
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.
Generated:
$map = new League\MimeTypeDetection\GeneratedExtensionToMimeTypeMap();
// string mime-type or NULL
$mimeType = $map->lookupMimeType('png');
Overriding decorator
$innerMap = new League\MimeTypeDetection\GeneratedExtensionToMimeTypeMap();
$map = new League\MimeTypeDetection\OverridingExtensionToMimeTypeMap($innerMap, ['png' => 'custom/mimetype']);
// string "custom/mimetype"
$mimeType = $map->lookupMimeType('png');
Empty:
$map = new League\MimeTypeDetection\EmptyExtensionToMimeTypeMap();
// Always returns NULL
$mimeType = $map->lookupMimeType('png');