Class MimeType
MimeType Class
Handles MIME type operations and provides functionality for working with Multipurpose Internet Mail Extensions (MIME) types in the application. This class is responsible for MIME type detection and mapping file extensions to their corresponding MIME types.
Property Summary
-
$mimeTypes protected static
array<string, array<string>>
Array of MIME type mappings.
Method Summary
-
addMimeTypes() public static
Add new mime types for a given file extension.
-
getExtension() public static
Get the file extension associated with a given MIME type.
-
getMimeType() public static
Get the MIME type based on the file extension.
-
getMimeTypeForFile() public static
Get the MIME type for a given file path.
-
getMimeTypes() public static
Get the MIME types associated with a given file extension.
-
setMimeTypes() public static
Set MIME types for a given file extension.
Method Detail
addMimeTypes() ¶ public static
addMimeTypes(string $ext, array|string $mimeTypes): void
Add new mime types for a given file extension.
If the file extension already exists, the new mime types will be merged with the existing ones.
Parameters
-
string
$ext The file extension to associate with the mime types.
-
array|string
$mimeTypes The mime types to associate with the file extension.
Returns
void
getExtension() ¶ public static
getExtension(string $mimeType): string|null
Get the file extension associated with a given MIME type.
Parameters
-
string
$mimeType The MIME type for which to get the file extension.
Returns
string|null
getMimeType() ¶ public static
getMimeType(string $ext, string|null $default = null): string|null
Get the MIME type based on the file extension.
Parameters
-
string
$ext The file extension.
-
string|null
$default optional The default MIME type to return if the extension is not found. Defaults to null.
Returns
string|null
getMimeTypeForFile() ¶ public static
getMimeTypeForFile(string $path, string $default = 'application/octet-stream'): string
Get the MIME type for a given file path.
If the MIME type is not mapped to an extension then it will attempt to determine the MIME type of the file using the fileinfo extension.
Parameters
-
string
$path The file path for which to get the MIME type.
-
string
$default optional The default MIME type to return if the MIME type cannot be determined.
Returns
string
getMimeTypes() ¶ public static
getMimeTypes(string $ext): array|null
Get the MIME types associated with a given file extension.
Parameters
-
string
$ext The file extension to look up.
Returns
array|null
setMimeTypes() ¶ public static
setMimeTypes(string $ext, array|string $mimeTypes): void
Set MIME types for a given file extension.
This will overwrite any existing MIME types for the file extension.
Parameters
-
string
$ext The file extension.
-
array|string
$mimeTypes The MIME types to associate with the file extension.
Returns
void
Property Detail
$mimeTypes ¶ protected static
Array of MIME type mappings.
Associates file extensions with their corresponding MIME type(s). Each key is a file extension (without the dot) and the value is an array of one or more valid MIME types for that extension.
Common MIME types included:
- Web formats (html, json, xml)
- Image formats (webp)
- Feed formats (rss)
- Application formats (ai, bin, csv, etc.)
Some extensions may map to multiple MIME types, with the first type in the array being the preferred/default type.
Type
array<string, array<string>>