Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/toverux/php-mime
(no longer maintained) Just a port of npm's mime package
https://github.com/toverux/php-mime
Last synced: 1 day ago
JSON representation
(no longer maintained) Just a port of npm's mime package
- Host: GitHub
- URL: https://github.com/toverux/php-mime
- Owner: toverux
- License: mit
- Created: 2015-07-17T18:36:00.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-04-22T18:36:27.000Z (over 8 years ago)
- Last Synced: 2024-04-22T19:09:07.103Z (7 months ago)
- Language: PHP
- Homepage:
- Size: 50.8 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PHP-Mime
[![SensioLabsInsight](https://img.shields.io/sensiolabs/i/e9103654-845f-40b7-8eeb-009e49e09067.svg?style=flat-square&label=insight)](https://insight.sensiolabs.com/projects/e9103654-845f-40b7-8eeb-009e49e09067)
[![Packagist (Composer)](https://img.shields.io/packagist/v/alembic/mime.svg?style=flat-square)](https://packagist.org/packages/alembic/mime)
![MIT License](https://img.shields.io/packagist/l/alembic/mime.svg?style=flat-square)Comprehensive MIME type mapping API, PHP clone of [broofa/node-mime](https://github.com/broofa/node-mime). MIME database from [jshttp/mime-db](https://github.com/jshttp/mime-db).
## Data sources
This library uses the mime-db repository. The updates are automatically pulled from their repository, using `bin/pull-mime-db.php [out] [--with-apache]`.By default this library exposes merged nginx + mime-db "custom types" (**119** usable mimes) and extensions. Those are the most commons types of the Internet. The library also includes the Apache types. A lot of Apache types are, in most part, useless and they are very numerous (**884** usable mimes), so, in order to reduce the memory footprint, Apache types are not loaded by default.
If you're searching for a library with more options and features, check [hoaproject/Mime](https://github.com/hoaproject/Mime). If you need something simple, this library is for you ! For example, this library is perfect to implement a HTTP server that serves static files with a mime type based on the extension - since MIME scanning is too slow.
## Install & Usage
composer require alembic/mime
```php
use Alembic\Mime\Mime;
```**Note:** All the methods exposed below can be called statically or with an instance of the `Mime` class (more practical in DI environments). Please note that, even when using instance calls, the MIMEs database is shared because it is static.
## API — Queries
### Mime::lookup($path [, $fallback])
Get the mime type associated with a file, if no mime type is found `$fallback` (`Mime::$defaultType` by default, which is `application/octet-stream`) is returned. Performs a case-insensitive lookup using the extension in `$path` (the substring after the last '.'). E.g.```php
use Alembic\Mime\Mime;Mime::lookup('/path/to/file.txt'); # => 'text/plain'
Mime::lookup('file.txt'); # => 'text/plain'
Mime::lookup('.TXT'); # => 'text/plain'
Mime::lookup('htm'); # => 'text/html'
Mime::lookup('unknown'); # => 'application/octet-stream'
Mime::lookup('unknown', null); # => null
# Instance mode:
(new Mime)->lookup('folder/file'); # => 'application/octet-stream'
```### Mime::$defaultType
The mime type returned when `Mime::lookup` fails to find the extension searched for (default is the standard `application/octet-stream`).### Mime::extension($type)
Get the preferred extension for `$type`.```php
Mime::extension('text/html'); # => 'html'
Mime::extension('application/octet-stream'); # => 'bin'
```### Mime::$defaultExtension
The extension returned when `Mime::extension` fails to find the type searched for (**warning**, default is `null`).## API — Defining Types
Custom type mappings can be added on a per-project basis via the following APIs.
### Mime::define()
Add custom mime/extension mappings.
```php
Mime::define([
'text/x-some-format' => ['x-sf', 'x-sft', 'x-sfml'],
'application/x-my-type' => ['x-mt', 'x-mtt'],
'application/x-my-format' => 'x-mf', # string allowed for unique ext
# etc ...
]);Mime::lookup('x-sft'); # => 'text/x-some-format'
```As said before, the first entry in the extensions array is returned by `Mime::extension()`. E.g.
```php
Mime::extension('text/x-some-format'); # => 'x-sf'
```### Mime::load($filepath)
Load mappings from an Apache ".types" file or a Nginx file containing a `types` block. The format (Nginx or Apache) is automagically detected based on the content.
Since the library uses `file_get_contents()`, the `$filepath` argument could be a filesystem path, an FTP path, an URL, whatever.
If the file couldn't be loaded (wrong path or insufficient privileges), it throws a `\RuntimeException`.```php
Mime::load('./my_project.types');
```
The Apache .types file or the Nginx `types` block format is simple — see the [examples](/examples) directory for examples.### Mime::apacheExtend()
Loads the packaged database of merged mime-db+nginx+Apache MIME types and extensions. The basic database (mime-db+nginx) is loaded by default and weighs 8 KiB, when the all-in-one database loaded by this method weighs 70 KiB.