Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
M

mime-type-detection

> 编程语言
Open source

League Mime Type Detection

1.3K stars0 likes0 views
WebsiteGitHub

About

League Mime Type Detection

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

Consumer interface

Your code is advised to couple to the following interfaces:

  • League\MimetypeDetection\MimeTypeDetector

    This contract is used to detect mimetypes based on file names and file contents.

  • League\MimetypeDetection\ExtensionLookup

    This contract is used to lookup one or all mimetypes for a given file extension. Added in 1.13.0.

Detectors

Finfo with extension fallback:

…

Extension only:

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

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
$allExtensions = $detector->lookupAllExtensions($mimeType);

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.

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');

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHP

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言