Oihana PHP

hasMimeType.php

Table of Contents

Functions

hasMimeType()  : bool
Checks whether a file's MIME type matches one of the given MIME types.

Functions

hasMimeType()

Checks whether a file's MIME type matches one of the given MIME types.

hasMimeType(string $filePath, string|array<string|int, string> $mimeTypes) : bool

The file's MIME type is detected with finfo and compared to each entry of $mimeTypes using a substring match (via str_contains()), so a partial type such as application/zip matches a detected application/zip; charset=binary.

Parameters
$filePath : string

Path to the file.

$mimeTypes : string|array<string|int, string>

A single MIME type, or a list of MIME types (or fragments) to match against.

Tags
author

Marc Alcaraz (ekameleon)

since
1.2.0
example
use function oihana\files\hasMimeType;

var_dump( hasMimeType('/path/to/archive.zip', 'application/zip'   ) ); // bool(true)
var_dump( hasMimeType('/path/to/archive.zip', ['application/zip'] ) ); // bool(true)
var_dump( hasMimeType('/path/to/notes.txt'  , ['application/zip'] ) ); // bool(false)
var_dump( hasMimeType('/path/to/missing'    , ['application/zip'] ) ); // bool(false)
Return values
bool

True if the file exists and its detected MIME type contains one of $mimeTypes.

On this page

Search results