Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bayfrontmedia/php-mime-types
Simple class used to detect the appropriate MIME type of a file based on it's extension.
https://github.com/bayfrontmedia/php-mime-types
extension file mime php type
Last synced: about 6 hours ago
JSON representation
Simple class used to detect the appropriate MIME type of a file based on it's extension.
- Host: GitHub
- URL: https://github.com/bayfrontmedia/php-mime-types
- Owner: bayfrontmedia
- License: mit
- Created: 2020-07-27T18:49:50.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-01-26T17:13:26.000Z (almost 2 years ago)
- Last Synced: 2024-10-11T10:50:02.199Z (27 days ago)
- Topics: extension, file, mime, php, type
- Language: PHP
- Homepage:
- Size: 12.7 KB
- Stars: 2
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
## PHP MIME types
Simple class used to detect appropriate MIME type.
This is by no means meant to handle an exhaustive list of every single MIME type, but rather focuses on the [most common MIME types](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types) used.
Since MIME types will be detected using the file extension, some file extension related methods are available to use as well.
- [License](#license)
- [Author](#author)
- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)## License
This project is open source and available under the [MIT License](LICENSE).
## Author
- [Bayfront Media homepage](https://www.bayfrontmedia.com?utm_source=github&utm_medium=direct)
- [Bayfront Media GitHub](https://github.com/bayfrontmedia)## Requirements
* PHP `^8.0`
## Installation
```
composer require bayfrontmedia/php-mime-types
```## Usage
- [getMimeTypes](#getmimetypes)
- [addMimeType](#addmimetype)
- [getExtension](#getextension)
- [hasExtension](#hasextension)
- [fromExtension](#fromextension)
- [fromFile](#fromfile)
### getMimeTypes
**Description:**
Return array of all MIME types.
**Parameters:**
- None
**Returns:**
- (array)
**Example:**
```
use Bayfront\MimeTypes\MimeType;print_r(MimeType::getMimeTypes());
```
### addMimeType
**Description:**
Adds new MIME type definitions.
**Parameters:**
- `$types` (array): Array whose keys are the file extension and values are the MIME type string
**Returns:**
- (void)
**Example:**
```
use Bayfront\MimeTypes\MimeType;MimeType::addMimeType([
'acgi' => 'text/html',
'avs' => 'video/avs-video'
]);
```
### getExtension
**Description:**
Return extension of a given file, or empty string if not existing.
**Parameters:**
- `$file` (string)
**Returns:**
- (string)
**Example:**
```
use Bayfront\MimeTypes\MimeType;echo MimeType::getExtension('pretty-photo.jpg');
```
### hasExtension
**Description:**
Checks if a file has a given extension.
**Parameters:**
- `$extension` (string)
- `$file` (string)#### Returns:
- (bool)
**Example:**
```
use Bayfront\MimeTypes\MimeType;if (MimeType::hasExtension('jpg', 'pretty-photo.jpg') {
// Do something
}
```
### fromExtension
**Description:**
Get MIME type from file extension.
**Parameters:**
- `$extension` (string)
- `$default = 'application/octet-stream'` (string): Default MIME type to return if none found for given extension**Returns:**
- (string)
**Example:**
```
use Bayfront\MimeTypes\MimeType;echo MimeType::fromExtension('jpg');
```
### fromFile
**Description:**
Get MIME type from file name.
**Parameters:**
- `$file` (string)
- `$default = 'application/octet-stream'` (string): Default MIME type to return if none found for given extension**Returns:**
- (string)
**Example:**
```
use Bayfront\MimeTypes\MimeType;echo MimeType::fromFile('pretty-photo.jpg');
```