Skip to content

FileMediaType

Viames Marino edited this page Apr 28, 2026 · 2 revisions

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

  • audio
  • binary
  • csv
  • document
  • flash
  • image
  • pdf
  • presentation
  • spreadsheet
  • video
  • zip

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.

Clone this wiki locally