Get MIME Type with PHP

A file MIME type is a two-part identifier for file formats. The two parts are separated by a / (forward slash) and describe what the file is – the first part is the type of file and the second is the subtype. A typical file MIME will look like this:

 

image/gif

 

In this tutorial, we will learn how to get the MIME type of a file with PHP.

 

Get the MIME Type

To get the MIME type of a file with PHP, use the mime_content_type() function. Pass the full path to file to check as the first argument and store the result in a variable.

 

$file = 'path/to/file.txt';

$mime = mime_content_type($file);
text/plain

 

Laravel MIME Type Example

Here is an example, of getting an uploaded file and checking its MIME type in Laravel:

 

$file = Input::file('upload');
$mime = mime_content_type($file->getRealPath());
file