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.
-
Operating System: Linux | Windows.
-
PHP versions: 8.0 | 8.1 | 8.2.
The preferred way to install this extension is through Composer.
To install PHP MimeType library, simply:
composer require josantonius/mime-typeThe 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-sourceYou can also clone the complete repository with Git:
git clone https://github.com/josantonius/php-mime-type.gitJosantonius\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;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;Example of use for this library:
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"
(...)
]use Josantonius\MimeType\MimeType;
$mimeType = new MimeType();
$mimeType->getExtension('text/html'); // .htmluse Josantonius\MimeType\MimeTypeCollection;
MimeTypeCollection::getExtension('application/zip'); // .zipuse Josantonius\MimeType\MimeType;
$mimeType = new MimeType();
$mimeType->getMime('.tar'); // application/x-tar
// The dot can be omitted:
$mimeType->getMime('mp4'); // video/mp4use Josantonius\MimeType\MimeTypeCollection;
MimeTypeCollection::getMime('.json'); // application/jsonTo run tests you just need composer and to execute the following:
git clone https://github.com/josantonius/php-mime-type.gitcd PHP-MimeTypecomposer installRun unit tests with PHPUnit:
composer phpunitRun code standard tests with PHPCS:
composer phpcsRun PHP Mess Detector tests to detect inconsistencies in code style:
composer phpmdRun all previous tests:
composer tests- Add new feature
- Improve tests
- Improve documentation
- Improve English translation in the README file
- Refactor code for disabled code style rules (see phpmd.xml and phpcs.xml)
Detailed changes for each release are documented in the release notes.
Please make sure to read the Contributing Guide, before making a pull request, start a discussion or report a issue.
Thanks to all contributors! ❤️
If this project helps you to reduce your development time, you can sponsor me to support my open source work 😊
This repository is licensed under the MIT License.
Copyright © 2016-2023, Josantonius