DEPRECATED: This project is no longer actively maintained. Please see Righteous MIMEs instead.
A comprehensive MIME and file extension tool for PHP. Finally!
It should be simple, but MIME types and file extensions are wildly inconsistent. Attempting to derive and/or correct that information when, e.g. verifying a file upload, is a nightmare in any programming language.
blob-mimes is a PHP library with a comprehensive MIME/ext database and simple helpers to access that information in a variety of ways:
- Pull MIME information based on a file name or extension;
- Pull file extensions for a given MIME type;
- Pull MIME information from a local file and verify its extension matches the actual content;
The database is compiled from the following sources:
blob-mimes requires PHP 7.0+ with the following modules:
- BCMath
- DOM
- Fileinfo
- Filter
- JSON
- MBString
- SimpleXML
UTF-8 is used for all string encoding. This could create conflicts on environments using something else.
The build script additionally requires cURL.
Install via Composer:
composer require "blobfolio/blob-mimes:dev-master"
Verify that a file extension and MIME type belong together. Because technology is always evolving and the MIME standard is always changing, this will consider some/thing
and some/x-thing
equivalent.
- (string) File extension
- (string) MIME type
- (bool) (optional) Soft pass. If
TRUE
, the check will returnTRUE
if it lacks information about either the extension or MIME type.
Returns TRUE
or FALSE
.
$foo = blobfolio\mimes\mimes::check_ext_and_mime('jpeg', 'image/jpeg'); //TRUE
$foo = blobfolio\mimes\mimes::check_ext_and_mime('jpeg', 'image/gif'); //FALSE
Pull path and type information for a file, using its name and/or content. If it is determined that the file is incorrectly named, alternative names with the correct file extension(s) are suggested.
- (string) Path
- (string) (optional) Nice name. If provided, the nice name will be treated as the filename. This can be useful if passing a temporary upload, for example. Default:
NULL
Returns all file information that can be derived according to the format below.
print_r(blobfolio\mimes\mimes::finfo('../wp/img/blobfolio.svg'));
/*
array(
[dirname] => /var/www/blob-common/wp/img
[basename] => blobfolio.svg
[extension] => svg
[filename] => blobfolio
[path] => /var/www/blob-common/wp/img/blobfolio.svg
[mime] => image/svg+xml
[suggested_filename] => array()
)
*/
Retrieve information about a file extension.
- (string) File extension
Returns the information or FALSE
. Note: The first MIME entry will always be the official (or most conventional) media type.
print_r(blobfolio\mimes\mimes::get_extension('jpeg'));
/*
array(
[ext] => jpeg
[mime] => array(
0 => image/jpeg
1 => image/pjpeg
)
)
*/
Retrieve information about all known file extensions.
N/A
Returns a MIME database organized by extension.
Retrieve information about a MIME type.
- (string) MIME type
Returns the information or FALSE
.
print_r(blobfolio\mimes\mimes::get_mime('image/jpeg'));
/*
array(
[mime] => image/jpeg
[ext] => array(
0 => jpeg
1 => jpg
2 => jpe
)
[source] => array(
0 => Apache
1 => Nginx
2 => freedesktop.org
)
)
*/
Retrieve information about all known MIME types.
N/A
Returns a MIME database organized by type. Note: The first extension should be the most common for the type.
Copyright © 2018 Blobfolio, LLC <[email protected]>
This work is free. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <[email protected]>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
If you have found this work useful and would like to contribute financially, Bitcoin tips are always welcome! 1Af56Nxauv8M1ChyQxtBe1yvdp2jtaB1GF |