MIME Types

use Derafu\File\Mime;
Method What it does
Mime::fromFile($path) The MIME type of a file, by its content.
Mime::fromContent($content) The MIME type of a content in memory.
Mime::typesOfExtension($extension) The MIME types that an extension can have.

What can not be told is null, not false, and none of them throws.

Of a file

Mime::fromFile('/var/app/uploads/document');   // 'application/pdf'

The type is detected by the content of the file, not by its name: an image named photo.txt is image/jpeg. A symbolic link is followed, and an empty file is application/x-empty. A content that has no known type is application/octet-stream.

It is null when the path is not a regular file that can be read (it does not exist, it is a directory, a broken link, a file without permission, an empty path or a path with a null character), or when the type can not be determined.

Of a content

Mime::fromContent($content);   // The same type as the file that has this content.
Mime::fromContent('');         // 'application/x-empty'

Only the content that is given is read, so it can be just the start of a file. That is enough for the formats that have a signature at the start (images, PDF, ZIP), but not for those that need the end of the file to be known: a .docx is detected as application/zip with only a start.

Of an extension

Mime::typesOfExtension('jpg');    // ['image/jpeg', 'image/pjpeg']
Mime::typesOfExtension('.CSV');   // ['text/csv', ...]
Mime::typesOfExtension('nope');   // []

An extension can have more than one type, as its aliases. The case and one leading point do not matter. An extension that is empty or unknown is the empty list.

On this page

Last updated on 05/11/2026 by Anonymous