Oihana PHP

getMimeType.php

Table of Contents

Functions

getMimeType()  : string|null
Detects the MIME type of a file using the `finfo` extension.

Functions

getMimeType()

Detects the MIME type of a file using the `finfo` extension.

getMimeType(string $file) : string|null

This is the low-level primitive shared by the higher-level MIME helpers (hasMimeType(), getImageMimeType(), tarFileInfo(), zipFileInfo()). It returns the raw type reported by finfo (e.g. text/plain, application/zip) without any normalization.

The function never throws: it returns null when the path is not a file or when detection fails (including an empty result), so callers can decide how to react (a boolean check, a fallback label such as 'unknown', etc.).

Parameters
$file : string

Path to the file to inspect.

Tags
author

Marc Alcaraz (ekameleon)

since
1.2.0
example
use function oihana\files\getMimeType;

var_dump( getMimeType('/path/to/notes.txt'  ) ); // string(10) "text/plain"
var_dump( getMimeType('/path/to/archive.zip') ); // string(15) "application/zip"
var_dump( getMimeType('/path/to/missing'    ) ); // NULL
Return values
string|null

The detected MIME type, or null if $file is not a file or detection failed.

On this page

Search results