PHPackages                             awaisjameel/mimetypes - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. awaisjameel/mimetypes

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

awaisjameel/mimetypes
=====================

A Laravel package for dynamically fetching, caching, and resolving MIME types and file extensions using the latest Apache MIME types list. Perfect for file upload validation and content-type detection.

v2.0.0(4w ago)076[4 PRs](https://github.com/awaisjameel/MimeTypes/pulls)1MITPHPPHP ^8.2CI passing

Since Feb 3Pushed 4w ago1 watchersCompare

[ Source](https://github.com/awaisjameel/MimeTypes)[ Packagist](https://packagist.org/packages/awaisjameel/mimetypes)[ Docs](https://github.com/awaisjameel/mimetypes)[ GitHub Sponsors](https://github.com/AwaisJameel)[ RSS](/packages/awaisjameel-mimetypes/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (1)Dependencies (26)Versions (8)Used By (1)

MimeTypes
=========

[](#mimetypes)

[![Latest Version on Packagist](https://camo.githubusercontent.com/1450755ad729852edb5d7b336cc8c8849993d6ab79d163fd81a4cb8a10c4aa5a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f61776169736a616d65656c2f6d696d6574797065732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/awaisjameel/mimetypes)[![GitHub Tests Action Status](https://camo.githubusercontent.com/2c9ec222ea86477419ea4a236d64d083ec710bfdeaa9ba9f7ba49d802c283385/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f61776169736a616d65656c2f6d696d6574797065732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/awaisjameel/mimetypes/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/b9a7ace2668e6222b042b4ae2fcb8bff9c95782d4266382da18c9e07e68ef359/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f61776169736a616d65656c2f6d696d6574797065732f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/awaisjameel/mimetypes/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/e1f78799f44e81ce0c0c0511ab794a132f796b5417ba1415f3c309469a32c0d1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f61776169736a616d65656c2f6d696d6574797065732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/awaisjameel/mimetypes)

**MimeTypes** is a Laravel package for resolving MIME types and file extensions against the canonical [Apache `mime.types`](https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types) list — the same public-domain dataset used by Apache HTTP Server, cURL, and countless other tools.

The list is fetched over HTTP, cached for as long as you like, and — unlike a plain HTTP call — never leaves your application without an answer: if the origin is unreachable, MimeTypes transparently falls back to a snapshot bundled with the package, so a network hiccup can never break file upload validation or content-type detection in production.

Features
--------

[](#features)

- **Bi-directional resolution** — MIME type → extension(s) and extension → MIME type.
- **Cached** — the resolved map is cached (TTL, store, and key are all configurable) so lookups never hit the network on the hot path.
- **Resilient by default** — falls back to a bundled snapshot of `mime.types` if the source URL is slow, down, or unreachable, instead of throwing.
- **Typed exceptions** — `UnknownMimeTypeOrExtensionException` and `UnableToFetchMimeTypesException` instead of a generic `Exception`, so you can catch precisely what you expect.
- **Dependency-injection friendly** — a proper injectable, container-bound service, not a static-only utility. Use the facade or inject it directly.
- **Zero-config out of the box**, fully configurable when you need it.

Requirements
------------

[](#requirements)

MimeTypesPHPLaravel^2.0^8.2^10.0, ^11.0, ^12.0, ^13.0Installation
------------

[](#installation)

Install the package via Composer:

```
composer require awaisjameel/mimetypes
```

The package's service provider and `MimeTypes` facade are auto-discovered — there's nothing else to register.

Optionally publish the config file:

```
php artisan vendor:publish --tag="mimetypes-config"
```

This is the config that gets published, with every option documented — see [Configuration](#configuration) below for what each one does:

```
return [
    'url' => env('MIMETYPES_URL', 'https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types'),
    'ttl' => (int) env('MIMETYPES_TTL', 1440),
    'cache_store' => env('MIMETYPES_CACHE_STORE'),
    'cache_key' => 'mimetypes.mime_type_map',
    'http' => [
        'timeout' => (int) env('MIMETYPES_HTTP_TIMEOUT', 5),
        'connect_timeout' => (int) env('MIMETYPES_HTTP_CONNECT_TIMEOUT', 3),
        'retry' => [
            'times' => (int) env('MIMETYPES_HTTP_RETRY_TIMES', 2),
            'sleep' => (int) env('MIMETYPES_HTTP_RETRY_SLEEP', 100),
        ],
    ],
    'fallback_to_bundled_snapshot' => (bool) env('MIMETYPES_FALLBACK_TO_BUNDLED_SNAPSHOT', true),
];
```

Usage
-----

[](#usage)

The recommended way to use the package is through its facade:

```
use AwaisJameel\MimeTypes\Facades\MimeTypes;

MimeTypes::toExtension('text/html');   // 'html'
MimeTypes::toMime('jpg');              // 'image/jpeg'
```

Prefer dependency injection? The underlying `AwaisJameel\MimeTypes\MimeTypes` class is bound as a singleton in the container, so you can type-hint it anywhere:

```
use AwaisJameel\MimeTypes\MimeTypes;

class AttachmentController
{
    public function __construct(private MimeTypes $mimeTypes)
    {
    }

    public function store()
    {
        $extension = $this->mimeTypes->toExtension($uploadedMime);
    }
}
```

### Resolving a MIME type from an extension

[](#resolving-a-mime-type-from-an-extension)

```
MimeTypes::toMime('jpg');   // 'image/jpeg'
MimeTypes::toMime('.jpg');  // 'image/jpeg' — a leading dot is stripped automatically
```

Throws `AwaisJameel\MimeTypes\Exceptions\UnknownMimeTypeOrExtensionException` if the extension isn't recognized.

### Resolving an extension from a MIME type

[](#resolving-an-extension-from-a-mime-type)

```
MimeTypes::toExtension('image/jpeg'); // 'jpeg' — the first/primary extension
```

Some MIME types map to more than one extension (`image/jpeg` → `jpeg`, `jpg`, `jpe`). To get all of them:

```
MimeTypes::extensionsFor('image/jpeg'); // ['jpeg', 'jpg', 'jpe']
```

### Checking whether a value is known

[](#checking-whether-a-value-is-known)

Useful for validation, where you want a boolean instead of a try/catch:

```
if (! MimeTypes::has($request->file('document')->getMimeType())) {
    abort(422, 'Unsupported file type.');
}
```

### Resolving either direction at once

[](#resolving-either-direction-at-once)

If you have a value that could be either a MIME type or an extension:

```
MimeTypes::getExtensionOrMime('text/html'); // 'html'
MimeTypes::getExtensionOrMime('html');      // 'text/html'
```

### Getting the full map

[](#getting-the-full-map)

```
MimeTypes::all(); // ['text/html' => ['html', 'htm'], 'image/jpeg' => ['jpeg', 'jpg', 'jpe'], ...]
```

### Forcing a refresh

[](#forcing-a-refresh)

The map is cached according to your [`ttl`](#configuration) config, but you can bypass the cache and refetch immediately — useful in a scheduled command that keeps the cache warm:

```
MimeTypes::refresh();
```

Exceptions
----------

[](#exceptions)

ExceptionThrown when`AwaisJameel\MimeTypes\Exceptions\UnknownMimeTypeOrExtensionException`The given MIME type or extension isn't in the resolved map.`AwaisJameel\MimeTypes\Exceptions\UnableToFetchMimeTypesException`The source URL couldn't be reached or returned an unusable response, **and** [`fallback_to_bundled_snapshot`](#configuration) is disabled.Both extend the package's base `AwaisJameel\MimeTypes\Exceptions\MimeTypesException`, so you can catch either specifically or catch everything the package throws in one place:

```
use AwaisJameel\MimeTypes\Exceptions\MimeTypesException;
use AwaisJameel\MimeTypes\Exceptions\UnknownMimeTypeOrExtensionException;
use AwaisJameel\MimeTypes\Facades\MimeTypes;

try {
    $extension = MimeTypes::toExtension($mime);
} catch (UnknownMimeTypeOrExtensionException) {
    $extension = 'bin';
} catch (MimeTypesException $e) {
    report($e);
    $extension = 'bin';
}
```

Configuration
-------------

[](#configuration)

KeyEnv variableDefaultDescription`url``MIMETYPES_URL`Apache's `mime.types`Source URL the MIME type list is fetched from. Must be in the standard `mime.types` format.`ttl``MIMETYPES_TTL``1440` (24 hours)Minutes the resolved map is cached for before being fetched again.`cache_store``MIMETYPES_CACHE_STORE``null` (default store)Which cache store to use.`cache_key`—`mimetypes.mime_type_map`The cache key the resolved map is stored under.`http.timeout``MIMETYPES_HTTP_TIMEOUT``5`Request timeout, in seconds.`http.connect_timeout``MIMETYPES_HTTP_CONNECT_TIMEOUT``3`Connection timeout, in seconds.`http.retry.times``MIMETYPES_HTTP_RETRY_TIMES``2`Number of attempts before giving up.`http.retry.sleep``MIMETYPES_HTTP_RETRY_SLEEP``100`Milliseconds to wait between attempts.`fallback_to_bundled_snapshot``MIMETYPES_FALLBACK_TO_BUNDLED_SNAPSHOT``true`Fall back to the bundled snapshot when the source URL can't be reached, instead of throwing `UnableToFetchMimeTypesException`.Testing your own code against this package
------------------------------------------

[](#testing-your-own-code-against-this-package)

Because `MimeTypes` is resolved through the container and its HTTP calls go through Laravel's `Http` client, you can fake the source in your own tests with `Http::fake()` rather than hitting the real Apache server:

```
use Illuminate\Support\Facades\Http;

Http::fake([
    '*' => Http::response("text/html\thtml htm\n"),
]);
```

Run this package's own test suite with:

```
composer test
```

Or with coverage:

```
composer test-coverage
```

Upgrading from 1.x
------------------

[](#upgrading-from-1x)

Version 2.0 is a from-the-ground-up rewrite. If you're upgrading from 1.x:

- `MimeTypes::generateUpToDateMimeArray()` is now `MimeTypes::all()`.
- `MimeTypes::getExtensionOrMime()` is still available and behaves the same, but now throws `UnknownMimeTypeOrExtensionException` instead of a generic `Exception`.
- The config file gained several new keys (`url`, `cache_store`, `cache_key`, `http`, `fallback_to_bundled_snapshot`) — republish it with `--force` to see the new defaults, or add the keys you need manually.
- The previously broken `mime_types_ttl` config key (it was read from the wrong config namespace and silently ignored) is now `ttl`, and it works.
- `AwaisJameel\MimeTypes\MimeTypes` is no longer a static-only utility — its methods are regular instance methods, resolved as a singleton from the container. Static-style calls through `AwaisJameel\MimeTypes\Facades\MimeTypes` continue to work exactly as before.

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Awais Jameel](https://github.com/awaisjameel)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

46

—

FairBetter than 92% of packages

Maintenance94

Actively maintained with recent releases

Popularity12

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 54.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~531 days

Total

2

Last Release

29d ago

Major Versions

1.0.0 → v2.0.02026-07-20

PHP version history (2 changes)1.0.0PHP &gt;=7.4

v2.0.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/8478f5cd00831255bda5f4ab259a8761a8001142756ab90bf8804388a78b2036?d=identicon)[awaisjameel](/maintainers/awaisjameel)

---

Top Contributors

[![awaisjameel](https://avatars.githubusercontent.com/u/9046343?v=4)](https://github.com/awaisjameel "awaisjameel (12 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (5 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (5 commits)")

---

Tags

laravelAwaisJameelmimetypesmimetype-to-extensionextension-to-mimetype

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/awaisjameel-mimetypes/health.svg)

```
[![Health](https://phpackages.com/badges/awaisjameel-mimetypes/health.svg)](https://phpackages.com/packages/awaisjameel-mimetypes)
```

###  Alternatives

[codewithdennis/filament-select-tree

The multi-level select field enables you to make single selections from a predefined list of options that are organized into multiple levels or depths.

329575.9k35](/packages/codewithdennis-filament-select-tree)[rawilk/profile-filament-plugin

Profile &amp; MFA starter kit for filament.

3915.5k](/packages/rawilk-profile-filament-plugin)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
