PHP Classes

mimey PHP MIME Type Conversion: Convert between file extensions and MIME types

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-04-29 (5 months ago) RSS 2.0 feedNot enough user ratingsTotal: 40 All time: 10,888 This week: 59Up
Version License PHP version Categories
mimey 2.1.0MIT/X Consortium ...8.2.0Files and Folders, PHP 8
Description 

Author

This package can convert between file extensions and MIME types.

It can perform several types of operations with MIME types.

Currently, it can:

- Take a string with a file name extension and return the respective MIME type

- Take a string with a MIME type and return the most common file associated name extension

- Return all the recognized MIME types

- Add new types of MIME types and associated file name extensions

Innovation Award
PHP Programming Innovation award nominee
September 2023
Number 2
Some applications need to process many types of files, for instance, when validating and processing file uploads.

One first step to perform file type validation for applications that process files uploaded by the users is to check the extension of the file name.

However, this step is not sufficient to perform file type validation. It is a fast step because it checks the file name and does not need to read the contents.

PHP has the fileinfo extension that provides the function mime_content_type. This function can detect the file type detection by reading the file contents. However, it is limited to the MIME types that the current implementation of this extension supports.

This package provides an alternative approach just for detecting the MIME type of a file based on the file name. It is extensible. So, it can support adding more MIME types to the list of file name extensions it can recognize.



Manuel Lemos
Picture of Eric Sizemore
  Performance   Level  
Name: Eric Sizemore is available for providing paid consulting. Contact Eric Sizemore .
Classes: 15 packages by
Country: United States United States
Age: 37
All time rank: 15119 in United States United States
Week rank: 192 Down22 in United States United States Down
Innovation award
Innovation award
Nominee: 4x

Winner: 2x

Instructions

For anyone using v1.x of Mimey, please see the CHANGELOG and UPGRADING before upgrading to 2.0.0.

Documentation

Mimey

PHP package for converting file extensions to MIME types and vice versa.

Build Status Code Coverage Scrutinizer Code Quality PHPStan Tests Psalm Static analysis Type Coverage Psalm Level SymfonyInsight Latest Stable Version Downloads per Month License

This package uses [httpd]'s [mime.types] to generate a mapping of file extension to MIME type and the other way around. Click here to view the changelog from their svn: [changelog]

The mime.types file is parsed by bin/generate.php and converted into an optimized JSON object in dist/mime.types.min.json which is then wrapped by helper class MimeTypes.

Also provides a generated PHP enum with all mime types and methods to get the extension. Can also be used to get the enum value from an extension.

[httpd]: https://httpd.apache.org/docs/current/programs/httpd.html [mime.types]: https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types [changelog]: https://svn.apache.org/viewvc/httpd/httpd/trunk/docs/conf/mime.types?view=log

Usage

$mimes = new MimeTypes;

// Convert extension to MIME type:
$mimes->getMimeType('json'); // application/json

// Convert MIME type to extension:
$mimes->getExtension('application/json'); // json

Using the enum

$json = MimeType::ApplicationJson;
echo $json->getExtension(); // json
echo $json->value; // application/json

$html = MimeType::fromExtension('html');
echo $html->value; // text/html

MimeType::fromExtension('asdf'); // throws an InvalidArgumentException if the extension cannot be found

Getting All

It's rare, but some extensions have multiple MIME types:

// Get all MIME types for an extension:
$mimes->getAllMimeTypes('wmz'); // array('application/x-ms-wmz', 'application/x-msmetafile')

However, there are many MIME types that have multiple extensions:

// Get all extensions for a MIME type:
$mimes->getAllExtensions('image/jpeg'); // array('jpeg', 'jpg', 'jpe')

Custom Conversions

You can add custom conversions by changing the mapping that is given to MimeTypes.

There is a Mapping\Builder that can help with this:

use Esi\Mimey\Mapping\Builder;

// Create a builder using the built-in conversions as the basis.
$builder = Builder::create();

// Add a conversion. This conversion will take precedence over existing ones.
$builder->add('custom/mime-type', 'myextension');

$mimes = new MimeTypes($builder->getMapping());
$mimes->getMimeType('myextension'); // custom/mime-type
$mimes->getExtension('custom/mime-type'); // myextension

You can add as many conversions as you would like to the builder:

$builder->add('custom/mime-type', 'myextension');
$builder->add('foo/bar', 'foobar');
$builder->add('foo/bar', 'fbar');
$builder->add('baz/qux', 'qux');
$builder->add('cat/qux', 'qux');
...

Optimized Custom Conversion Loading

You can optimize the loading of custom conversions by saving all conversions to a compiled PHP file as part of a build step.

// Add a bunch of custom conversions.
$builder->add(...);
$builder->add(...);
$builder->add(...);
...
// Save the conversions to a cached file.
$builder->save($cacheFilePath);

The file can then be loaded to avoid overhead of repeated $builder->add(...) calls:

// Load the conversions from a cached file.
$builder = Builder::load($cacheFilePath);
$mimes = new MimeTypes($builder->getMapping());

Install

Compatible with PHP >= 8.2.

composer require esi/mimey

Credits

This fork uses the same license as the original repository by @ralouphie (MIT). This repository is a fork of elephox-dev/mimey which itself was a fork of ralouphie/mimey. Thanks to them and all the contributors!

Submitting bugs and feature requests

Bugs and feature requests are tracked on GitHub

Issues are the quickest way to report a bug. If you find a bug or documentation error, please check the following first:

  • That there is not an Issue already open concerning the bug
  • That the issue has not already been addressed (within closed Issues, for example)

Contributing

Missing a MIME type?

Open an issue or even add it yourself! The process is very easy:

  1. fork this repository
  2. add your MIME type to the `data/mime.types.custom` file (make sure it's properly formatted!)
  3. push your changes
  4. submit a pull request

More information for contributions in CONTRIBUTING.

Author

Eric Sizemore - <admin@secondversion.com> - <https://www.secondversion.com>

License

Mimey is licensed under the MIT License - see the LICENSE.md file for details.


  Files folder image Files (46)  
File Role Description
Files folder image.github (3 files, 2 directories)
Files folder imagebin (2 files)
Files folder imagedata (2 files)
Files folder imagedist (3 files)
Files folder imagesrc (1 file, 2 directories)
Files folder imagestubs (1 file)
Files folder imagetests (1 file, 1 directory)
Accessible without login Plain text file .php-cs-fixer.dist.php Aux. Auxiliary script
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file backward-compatibility.md Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file CODE_OF_CONDUCT.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file CONTRIBUTING.md Data Auxiliary data
Accessible without login Plain text file LICENSE.md Lic. License text
Accessible without login Plain text file phpstan-baseline.neon Data Auxiliary data
Accessible without login Plain text file phpstan.neon Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file psalm.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file renovate.json Data Auxiliary data
Accessible without login Plain text file SECURITY.md Data Auxiliary data
Accessible without login Plain text file UPGRADING.md Data Auxiliary data

  Files folder image Files (46)  /  .github  
File Role Description
Files folder imageISSUE_TEMPLATE (3 files)
Files folder imageworkflows (5 files)
  Accessible without login Plain text file dependabot.yml Data Auxiliary data
  Accessible without login Plain text file FUNDING.yml Data Auxiliary data
  Accessible without login Plain text file pull_request_template.md Data Auxiliary data

  Files folder image Files (46)  /  .github  /  ISSUE_TEMPLATE  
File Role Description
  Accessible without login Plain text file 1-bug_report.yml Data Auxiliary data
  Accessible without login Plain text file 2-feature_request.yml Data Auxiliary data
  Accessible without login Plain text file config.yml Data Auxiliary data

  Files folder image Files (46)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file ci.yml Data Auxiliary data
  Accessible without login Plain text file generate.yml Data Auxiliary data
  Accessible without login Plain text file psalm.yml Data Auxiliary data
  Accessible without login Plain text file tests.yml Data Auxiliary data
  Accessible without login Plain text file update.yml Data Auxiliary data

  Files folder image Files (46)  /  bin  
File Role Description
  Accessible without login Plain text file generate.php Example Example script
  Accessible without login Plain text file update.php Aux. Auxiliary script

  Files folder image Files (46)  /  data  
File Role Description
  Accessible without login Plain text file mime.types Data Auxiliary data
  Accessible without login Plain text file mime.types.custom Data Auxiliary data

  Files folder image Files (46)  /  dist  
File Role Description
  Accessible without login Plain text file mime.types.json Data Auxiliary data
  Accessible without login Plain text file mime.types.min.json Data Auxiliary data
  Accessible without login Plain text file MimeType.php Example Example script

  Files folder image Files (46)  /  src  
File Role Description
Files folder imageInterfaces (3 files)
Files folder imageMapping (2 files)
  Plain text file MimeTypes.php Class Class source

  Files folder image Files (46)  /  src  /  Interfaces  
File Role Description
  Plain text file BuilderInterface.php Class Class source
  Plain text file MimeTypeInterface.php Class Class source
  Plain text file MimeTypesInterface.php Class Class source

  Files folder image Files (46)  /  src  /  Mapping  
File Role Description
  Plain text file Builder.php Class Class source
  Plain text file Generator.php Class Class source

  Files folder image Files (46)  /  stubs  
File Role Description
  Accessible without login Plain text file mimeType.php.stub Example Example script

  Files folder image Files (46)  /  tests  
File Role Description
Files folder imagesrc (3 files)
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script

  Files folder image Files (46)  /  tests  /  src  
File Role Description
  Plain text file BuilderTest.php Class Class source
  Plain text file GeneratorTest.php Class Class source
  Plain text file MimeTypesTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:40
This week:0
All time:10,888
This week:59Up