PHP MimeType library

PHP library to get MIME types from extensions.
The original concept of my project, linking a single mimetype to a file extension, has been found to be insufficient in accurately reflecting the many-to-one relationships that exist in real-world use-cases. In practice, multiple file extensions often correspond to a single mimetype, rendering my existing design approach suboptimal.
Given these insights, I’ve decided to archive this repository. I recommend those interested to look at the approach taken by the repository patrickmccallum/mimetype-io. This project adopts a more nuanced perspective, effectively managing mimetypes by acknowledging the possibility of multiple extensions corresponding to a single mimetype. I believe this is a more promising approach for managing mimetypes moving forward.
Requirements
Installation
The preferred way to install this extension is through Composer.
To install PHP MimeType library, simply:
composer require josantonius/mime-type
The previous command will only install the necessary files,
if you prefer to download the entire source code you can use:
composer require josantonius/mime-type --prefer-source
You can also clone the complete repository with Git:
git clone https://github.com/josantonius/php-mime-type.git
Available Classes
MimeType
Josantonius\MimeType\MimeType
Get array with all MIME types:
public function all(): array;
Get file extension from MIME type:
public function getExtension(string $mimeType): string|null;
Get MIME type from file extension:
public function getMime(string $extension): string|null;
MimeTypeCollection
Josantonius\MimeType\MimeTypeCollection
Get array with all MIME types:
public static function all(): array;
Get file extension from MIME type:
public static function getExtension(string $mimeType): string|null;
Get MIME type from file extension:
public static function getMime(string $extension): string|null;
Usage
Example of use for this library:
Get array with all MIME types
use Josantonius\MimeType\MimeType;
$mimeType = new MimeType();
$mimeType->all();
use Josantonius\MimeType\MimeTypeCollection;
MimeTypeCollection::all();
Result:
[
[".123"]=> "application/vnd.lotus-1-2-3"
[".3dml"]=> "text/vnd.in3d.3dml"
[".3g2"]=> "video/3gpp2"
[".3gp"]=> "video/3gpp"
[".7z"]=> "application/x-7z-compressed"
[".aab"]=> "application/x-authorware-bin"
[".aac"]=> "audio/x-aac"
[".aam"]=> "application/x-authorware-map"
[".aas"]=> "application/x-authorware-seg"
[".abw"]=> "application/x-abiword"
(...)
]
Get file extension from MIME type
use Josantonius\MimeType\MimeType;
$mimeType = new MimeType();
$mimeType->getExtension('text/html'); // .html
use Josantonius\MimeType\MimeTypeCollection;
MimeTypeCollection::getExtension('application/zip'); // .zip
Get MIME type from file extension
use Josantonius\MimeType\MimeType;
$mimeType = new MimeType();
$mimeType->getMime('.tar'); // application/x-tar
// The dot can be omitted:
$mimeType->getMime('mp4'); // video/mp4
use Josantonius\MimeType\MimeTypeCollection;
MimeTypeCollection::getMime('.json'); // application/json
Tests
To run tests you just need composer
and to execute the following:
git clone https://github.com/josantonius/php-mime-type.git
cd PHP-MimeType
composer install
Run unit tests with PHPUnit:
composer phpunit
Run code standard tests with PHPCS:
composer phpcs
Run PHP Mess Detector tests to detect inconsistencies in code style:
composer phpmd
Run all previous tests:
composer tests
TODO
Changelog
Detailed changes for each release are documented in the
release notes.
Contribution
Please make sure to read the Contributing Guide, before making a pull
request, start a discussion or report a issue.
Thanks to all contributors! :heart:
If this project helps you to reduce your development time,
you can sponsor me to support my open source work :blush:
License
This repository is licensed under the MIT License.
Copyright © 2016-2023, Josantonius
评论