karelwintersky/arris.toolkit.mimetypes

Resolves Extension by MimeType and vice versa

1.99.0 2024-07-22 18:06 UTC

This package is auto-updated.

Last update: 2024-12-22 19:13:13 UTC


README

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

Uses the MIME content type for a file as determined by using information from the magic.mime file.

Mimetypes

Creates a JSON document containing a thorough list of file extensions => mime types as provided by the Apache httpd project.

How to update?

Step 1

Download the latest copy of the Apache mime-types file into the same directory as the generate script

make update

or

wget --no-check-certificate https://svn.apache.org/repos/asf/httpd/httpd/branches/2.4.x/docs/conf/mime.types -O ./tools/mime.types

Step 2 (optional)

Update the provided mime.types.custom. This file is in the same format as mime.types but the mappings in this file take precedence.

Update the provided customize.json document with any additional mimetypes to define, or any that you would like to override.

Step 3

Run the generate script:

make build

In the end, a mimetypes.json document will be generated. This JSON document can be easily parsed into a map/dictionary/associative array by pretty much every programming language with little effort.

It also generates a backing PHP class if you want to use the data in PHP-land.

Step 4

make test

Step 5

Update repository (commit, push, pull request)

Installation

Install with Composer

If you're using Composer to manage dependencies, you can add the mimetypes with it.

{
    "require": {
        "karelwintersky/arris.toolkit.mimetypes": ">=1.0"
    }
}

or

composer require karelwintersky/arris.toolkit.mimetypes

Usage

And use it in your scripts:

use Arris\Toolkit;
$ext = MimeTypes::getExtension('application/json');

// => .json

$type = MimeTypes::fromExtension('.json');

// 'application/json'

NB:

'fallback' function mime_content_type() return all same data.

License & Copyright

Original idea: 2010-2013 Ryan Parman.

Refactored: 2023 Karel Wintersky

removed unnecessary Twig template engine and dflydev/apache-mime-types (repository load/mapper)

Licensed for use under the terms of the MIT license.