Get MIME Type From File Extension Using PHP

Introduction

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 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
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(
	 'txt' => 'text/plain',
	 'htm' => 'text/html',
	 'html' => 'text/html',
	 'css' => 'text/css',
	 'json' => array('application/json', 'text/json'),
	 'xml' => 'application/xml',
	 'swf' => 'application/x-shockwave-flash',
	 'flv' => 'video/x-flv',

	 'hqx' => 'application/mac-binhex40',
	 'cpt' => 'application/mac-compactpro',
	 'csv' => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel'),
	 'bin' => 'application/macbinary',
	 'dms' => 'application/octet-stream',
	 'lha' => 'application/octet-stream',
	 'lzh' => 'application/octet-stream',
	 'exe' => array('application/octet-stream', 'application/x-msdownload'),
	 'class' => 'application/octet-stream',
	 'so' => 'application/octet-stream',
	 'sea' => 'application/octet-stream',
	 'dll' => 'application/octet-stream',
	 'oda' => 'application/oda',
	 'ps' => 'application/postscript',
	 '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',
	 'php' => 'application/x-httpd-php',
	 'php4' => 'application/x-httpd-php',
	 'php3' => 'application/x-httpd-php',
	 'phtml' => 'application/x-httpd-php',
	 'phps' => 'application/x-httpd-php-source',
	 'js' => array('application/javascript', 'application/x-javascript'),
	 'sit' => 'application/x-stuffit',
	 'tar' => 'application/x-tar',
	 'tgz' => array('application/x-tar', 'application/x-gzip-compressed'),
	 'xhtml' => 'application/xhtml+xml',
	 'xht' => 'application/xhtml+xml',             
	 'bmp' => array('image/bmp', 'image/x-windows-bmp'),
	 'gif' => 'image/gif',
	 'jpeg' => array('image/jpeg', 'image/pjpeg'),
	 'jpg' => array('image/jpeg', 'image/pjpeg'),
	 'jpe' => array('image/jpeg', 'image/pjpeg'),
	 'png' => array('image/png', 'image/x-png'),
	 'tiff' => 'image/tiff',
	 'tif' => 'image/tiff',
	 'shtml' => 'text/html',
	 'text' => 'text/plain',
	 'log' => array('text/plain', 'text/x-log'),
	 'rtx' => 'text/richtext',
	 'rtf' => 'text/rtf',
	 'xsl' => 'text/xml',
	 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
	 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
	 'word' => array('application/msword', 'application/octet-stream'),
	 'xl' => 'application/excel',
	 'eml' => 'message/rfc822',

	 // images
	 'png' => 'image/png',
	 'jpe' => 'image/jpeg',
	 'jpeg' => 'image/jpeg',
	 'jpg' => 'image/jpeg',
	 'gif' => 'image/gif',
	 'bmp' => 'image/bmp',
	 'ico' => 'image/vnd.microsoft.icon',
	 'tiff' => 'image/tiff',
	 'tif' => 'image/tiff',
	 'svg' => 'image/svg+xml',
	 'svgz' => 'image/svg+xml',

	 // archives
	 'zip' => array('application/x-zip', 'application/zip', 'application/x-zip-compressed'),
	 'rar' => 'application/x-rar-compressed',
	 'msi' => 'application/x-msdownload',
	 'cab' => 'application/vnd.ms-cab-compressed',

	 // audio/video
	 'mid' => 'audio/midi',
	 'midi' => 'audio/midi',
	 'mpga' => 'audio/mpeg',
	'mp2' => 'audio/mpeg',
	 'mp3' => array('audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3'),
	 'aif' => 'audio/x-aiff',
	 'aiff' => 'audio/x-aiff',
	 'aifc' => 'audio/x-aiff',
	 'ram' => 'audio/x-pn-realaudio',
	 'rm' => 'audio/x-pn-realaudio',
	 'rpm' => 'audio/x-pn-realaudio-plugin',
	 'ra' => 'audio/x-realaudio',
	 'rv' => 'video/vnd.rn-realvideo',
	 'wav' => array('audio/x-wav', 'audio/wave', 'audio/wav'),
	 'mpeg' => 'video/mpeg',
	 'mpg' => 'video/mpeg',
	 'mpe' => 'video/mpeg',
	 'qt' => 'video/quicktime',
	 'mov' => 'video/quicktime',
	 'avi' => 'video/x-msvideo',
	 'movie' => 'video/x-sgi-movie',

	 // adobe
	 'pdf' => 'application/pdf',
	 'psd' => array('image/vnd.adobe.photoshop', 'application/x-photoshop'),
	 'ai' => 'application/postscript',
	 'eps' => 'application/postscript',
	 'ps' => 'application/postscript',

	 // ms office
	 'doc' => 'application/msword',
	 'rtf' => 'application/rtf',
	 'xls' => array('application/excel', 'application/vnd.ms-excel', 'application/msexcel'),
	 'ppt' => array('application/powerpoint', 'application/vnd.ms-powerpoint'),

	 // open office
	 'odt' => 'application/vnd.oasis.opendocument.text',
	 'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
  );

  $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.