PHP Classes

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

Recommend this page to a friend!
  Info   View files Documentation   View files View files (35)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-29 (23 days ago) RSS 2.0 feedNot enough user ratingsTotal: 35 All time: 10,896 This week: 39Up
Version License PHP version Categories
mimey 1.2.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: 9 packages by
Country: United States United States
Age: 36
All time rank: 15319 in United States United States
Week rank: 36 Up4 in United States United States Up
Innovation award
Innovation award
Nominee: 2x

Documentation

Mimey

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

FOSSA Status Build Status Code Coverage Scrutinizer Code Quality PHPStan Tests Psalm Security Scan

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 MimeMappingBuilder that can help with this:

// Create a builder using the built-in conversions as the basis.
$builder = MimeMappingBuilder::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 = MimeMappingBuilder::load($cacheFilePath);
$mimes = new MimeTypes($builder->getMapping());

Install

Compatible with PHP >= 8.2.

composer require esi/mimey

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

After a short review and merge, the MIME type will automagically be added to the library.

If you want to, you can also run composer generate-types and add the changed files under dist/ to your PR.

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!

License

FOSSA Status


  Files folder image Files  
File Role Description
Files folder image.github (2 files, 1 directory)
Files folder imagebin (2 files)
Files folder imagedata (2 files)
Files folder imagedist (3 files)
Files folder imagesrc (5 files)
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 CHANGELOG.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 LICENSE.md Lic. License text
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 README.md Doc. Documentation
Accessible without login Plain text file rector.php Aux. Auxiliary script
Accessible without login Plain text file SECURITY.md Data Auxiliary data

  Files folder image Files  /  .github  
File Role Description
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

  Files folder image Files  /  .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  /  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  /  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  /  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  /  src  
File Role Description
  Plain text file MimeMappingBuilder.php Class Class source
  Plain text file MimeMappingGenerator.php Class Class source
  Plain text file MimeTypeInterface.php Class Class source
  Plain text file MimeTypes.php Class Class source
  Plain text file MimeTypesInterface.php Class Class source

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

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

  Files folder image Files  /  tests  /  src  
File Role Description
  Plain text file MimeMappingBuilderTest.php Class Class source
  Plain text file MimeMappingGeneratorTest.php Class Class source
  Plain text file MimeTypesTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:35
This week:0
All time:10,896
This week:39Up