FileMediaType - viames/pair GitHub Wiki
Pair framework: FileMediaType
Pair\Http\FileMediaType is the shared MIME type and file-extension category registry used by upload handling and file input controls.
Methods
categoryTypes(string $category): array
Returns all MIME types and extensions for a category.
category(string $mediaType): ?string
Returns the first category matching a MIME type or file extension.
categories(string $mediaType): array
Returns every category matching a MIME type or file extension.
matchesCategory(string $mediaType, string $category): bool
Returns whether a MIME type or extension belongs to a specific category. Use this for validation when a MIME type can belong to more than one category, such as text/plain.
Categories
audiobinarycsvdocumentflashimagepdfpresentationspreadsheetvideozip
Examples
Category lookup
use Pair\Http\FileMediaType;
$category = FileMediaType::category('application/pdf');
// 'pdf'
$acceptValues = FileMediaType::categoryTypes('image');
if (FileMediaType::matchesCategory('text/plain', 'csv')) {
// Accept CSV files detected by servers as text/plain.
}
Upload category validation
use Pair\Http\FileMediaType;
use Pair\Http\UploadedFile;
$upload = UploadedFile::fromGlobals('attachment');
$mediaType = $upload->mediaType();
if (!is_string($mediaType) || !FileMediaType::matchesCategory($mediaType, 'document')) {
throw new \RuntimeException('Attachment must be a document.');
}
See also: UploadedFile, File.