Get MIME Type From File Extension Using PHP

MIME Type

This tutorial will show you how you can get MIME type of a file using PHP program. Most of the times you may need to validate or check MIME type of a file when you upload a file for various reasons, such as, for KYC (Know Your Customer) purpose.

In this tutorial I have defined an array which holds almost all kinds of MIME type of files. So whenever you require to get the MIME type of a particular file then you can easily retrieve the MIME type of that file from this array.

Prerequisites

PHP 5.4/7.4.22/8.2.7
Apace http Server 2.4 (Optional)

Project Directory

It’s assumed that you have setup PHP in your system.

Now I will create a project root directory called php-file-mime anywhere in your system. I may not mention the project root directory in subsequent sections and I will assume that I am talking with respect to the project’s root directory.

Get MIME Type Of File

The following code checks the mime type for a given file name and returns the MIME type of the file. The following code is written into a file php-file-mime.php.

function mime_type($filename) {

  $mime_types = array(
	 ...
	 'smi' => 'application/smil',
	 'smil' => 'application/smil',
	 'mif' => 'application/vnd.mif',
	 'wbxml' => 'application/wbxml',
	 'wmlc' => 'application/wmlc',
	 'dcr' => 'application/x-director',
	 'dir' => 'application/x-director',
	 'dxr' => 'application/x-director',
	 'dvi' => 'application/x-dvi',
	 'gtar' => 'application/x-gtar',
	 'gz' => 'application/x-gzip',
	 ...
  );

  $ext = explode('.', $filename);
  $ext = strtolower(end($ext));
 
  if (array_key_exists($ext, $mime_types)) {
	return (is_array($mime_types[$ext])) ? $mime_types[$ext][0] : $mime_types[$ext];
  } else if (function_exists('finfo_open')) {
	 if(file_exists($filename)) {
	   $finfo = finfo_open(FILEINFO_MIME);
	   $mimetype = finfo_file($finfo, $filename);
	   finfo_close($finfo);
	   return $mimetype;
	 }
  }
 
  return 'application/octet-stream';
}

If multiple MIME types found for a particular file then the first MIME type is returned. If no MIME found for a particular file then default MIME type application/octet-stream is returned.

Usage

Here are some examples how to use the above function to get MIME type of a file:

$mime_type = mime_type('a.php');
echo $mime_type . "\n";
 
$mime_type = mime_type('final.doc');
echo $mime_type . "\n";
 
$mime_type = mime_type('inception.pdf');
echo $mime_type . "\n";
 
$mime_type = mime_type('meeting.jpg');
echo $mime_type . "\n";
 
$mime_type = mime_type('filename.xxx');
echo $mime_type . "\n";
 
$mime_type = mime_type('a.csv');
echo $mime_type . "\n";
 
$mime_type = mime_type('a.xl');
echo $mime_type . "\n";

The above code snippets will output as given in the below:

application/x-httpd-php
application/msword
application/pdf
image/jpeg
application/octet-stream
text/x-comma-separated-values
application/excel

Source Code

Download

Leave a Reply

Your email address will not be published. Required fields are marked *