Enquiry Email Form

You’ve probably landed on this page because you want to find the Content-type of a file by using the mime_content_type function. But when you tried using the function, you were returned with a PHP error because it’s a depreciated function (PHP 4). Then you may have tried to use the Fileinfo function (am alternative suggested by the PHP website), but then realised you haven’t got the PHP Fileinfo extension/library installed (because you were returned with an error again). So what’s your alternative?

Possible solutions

  • Firstly, if you’re running off a dedicated sever or VPS, you could install the Fileinfo extension. However, if you’re on hosted on a shared server, your hosting provider may not install the extension for you. But it might be worth trying to contact them to get it installed first.
  • The second solution is to write your own function, which is the code I’m going to provide in this blog post

Write your own mime_content_type Function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
if (!function_exists('mime_content_type')) {
	function mime_content_type($filename) {
	$idx = explode( '.', $filename );
	$count_explode = count($idx);
	$idx = strtolower($idx[$count_explode-1]);
 
	$mimet = array(	
        'ai' =>'application/postscript',
	'aif' =>'audio/x-aiff',
	'aifc' =>'audio/x-aiff',
	'aiff' =>'audio/x-aiff',
	'asc' =>'text/plain',
	'atom' =>'application/atom+xml',
	'avi' =>'video/x-msvideo',
	'bcpio' =>'application/x-bcpio',
	'bmp' =>'image/bmp',
	'cdf' =>'application/x-netcdf',
	'cgm' =>'image/cgm',
	'cpio' =>'application/x-cpio',
	'cpt' =>'application/mac-compactpro',
	'crl' =>'application/x-pkcs7-crl',
	'crt' =>'application/x-x509-ca-cert',
	'csh' =>'application/x-csh',
	'css' =>'text/css',
	'dcr' =>'application/x-director',
	'dir' =>'application/x-director',
	'djv' =>'image/vnd.djvu',
	'djvu' =>'image/vnd.djvu',
	'doc' =>'application/msword',
	'dtd' =>'application/xml-dtd',
	'dvi' =>'application/x-dvi',
	'dxr' =>'application/x-director',
	'eps' =>'application/postscript',
	'etx' =>'text/x-setext',
	'ez' =>'application/andrew-inset',
	'gif' =>'image/gif',
	'gram' =>'application/srgs',
	'grxml' =>'application/srgs+xml',
	'gtar' =>'application/x-gtar',
	'hdf' =>'application/x-hdf',
	'hqx' =>'application/mac-binhex40',
	'html' =>'text/html',
	'html' =>'text/html',
	'ice' =>'x-conference/x-cooltalk',
	'ico' =>'image/x-icon',
	'ics' =>'text/calendar',
	'ief' =>'image/ief',
	'ifb' =>'text/calendar',
	'iges' =>'model/iges',
	'igs' =>'model/iges',
	'jpe' =>'image/jpeg',
	'jpeg' =>'image/jpeg',
	'jpg' =>'image/jpeg',
	'js' =>'application/x-javascript',
	'kar' =>'audio/midi',
	'latex' =>'application/x-latex',
	'm3u' =>'audio/x-mpegurl',
	'man' =>'application/x-troff-man',
	'mathml' =>'application/mathml+xml',
	'me' =>'application/x-troff-me',
	'mesh' =>'model/mesh',
	'mid' =>'audio/midi',
	'midi' =>'audio/midi',
	'mif' =>'application/vnd.mif',
	'mov' =>'video/quicktime',
	'movie' =>'video/x-sgi-movie',
	'mp2' =>'audio/mpeg',
	'mp3' =>'audio/mpeg',
	'mpe' =>'video/mpeg',
	'mpeg' =>'video/mpeg',
	'mpg' =>'video/mpeg',
	'mpga' =>'audio/mpeg',
	'ms' =>'application/x-troff-ms',
	'msh' =>'model/mesh',
	'mxu m4u' =>'video/vnd.mpegurl',
	'nc' =>'application/x-netcdf',
	'oda' =>'application/oda',
	'ogg' =>'application/ogg',
	'pbm' =>'image/x-portable-bitmap',
	'pdb' =>'chemical/x-pdb',
	'pdf' =>'application/pdf',
	'pgm' =>'image/x-portable-graymap',
	'pgn' =>'application/x-chess-pgn',
	'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',
	'png' =>'image/png',
	'pnm' =>'image/x-portable-anymap',
	'ppm' =>'image/x-portable-pixmap',
	'ppt' =>'application/vnd.ms-powerpoint',
	'ps' =>'application/postscript',
	'qt' =>'video/quicktime',
	'ra' =>'audio/x-pn-realaudio',
	'ram' =>'audio/x-pn-realaudio',
	'ras' =>'image/x-cmu-raster',
	'rdf' =>'application/rdf+xml',
	'rgb' =>'image/x-rgb',
	'rm' =>'application/vnd.rn-realmedia',
	'roff' =>'application/x-troff',
	'rtf' =>'text/rtf',
	'rtx' =>'text/richtext',
	'sgm' =>'text/sgml',
	'sgml' =>'text/sgml',
	'sh' =>'application/x-sh',
	'shar' =>'application/x-shar',
	'shtml' =>'text/html',
	'silo' =>'model/mesh',
	'sit' =>'application/x-stuffit',
	'skd' =>'application/x-koan',
	'skm' =>'application/x-koan',
	'skp' =>'application/x-koan',
	'skt' =>'application/x-koan',
	'smi' =>'application/smil',
	'smil' =>'application/smil',
	'snd' =>'audio/basic',
	'spl' =>'application/x-futuresplash',
	'src' =>'application/x-wais-source',
	'sv4cpio' =>'application/x-sv4cpio',
	'sv4crc' =>'application/x-sv4crc',
	'svg' =>'image/svg+xml',
	'swf' =>'application/x-shockwave-flash',
	't' =>'application/x-troff',
	'tar' =>'application/x-tar',
	'tcl' =>'application/x-tcl',
	'tex' =>'application/x-tex',
	'texi' =>'application/x-texinfo',
	'texinfo' =>'application/x-texinfo',
	'tgz' =>'application/x-tar',
	'tif' =>'image/tiff',
	'tiff' =>'image/tiff',
	'tr' =>'application/x-troff',
	'tsv' =>'text/tab-separated-values',
	'txt' =>'text/plain',
	'ustar' =>'application/x-ustar',
	'vcd' =>'application/x-cdlink',
	'vrml' =>'model/vrml',
	'vxml' =>'application/voicexml+xml',
	'wav' =>'audio/x-wav',
	'wbmp' =>'image/vnd.wap.wbmp',
	'wbxml' =>'application/vnd.wap.wbxml',
	'wml' =>'text/vnd.wap.wml',
	'wmlc' =>'application/vnd.wap.wmlc',
	'wmlc' =>'application/vnd.wap.wmlc',
	'wmls' =>'text/vnd.wap.wmlscript',
	'wmlsc' =>'application/vnd.wap.wmlscriptc',
	'wmlsc' =>'application/vnd.wap.wmlscriptc',
	'wrl' =>'model/vrml',
	'xbm' =>'image/x-xbitmap',
	'xht' =>'application/xhtml+xml',
	'xhtml' =>'application/xhtml+xml',
	'xls' =>'application/vnd.ms-excel',
	'xml xsl' =>'application/xml',
	'xpm' =>'image/x-xpixmap',
	'xslt' =>'application/xslt+xml',
	'xul' =>'application/vnd.mozilla.xul+xml',
	'xwd' =>'image/x-xwindowdump',
	'xyz' =>'chemical/x-xyz',
	'zip' =>'application/zip'
	);
 
	if (isset( $mimet[$idx] )) {
	 return $mimet[$idx];
	} else {
	 return 'application/octet-stream';
	}
    }
}
 
//$mm_type will return the content type (note, I still use the original function name mime_content_type)
$mm_type = mime_content_type($filepath);
 
?>

If anyone has a better solution of retrieving the content-type without using the depreciated mime_content_type function and the Fileinfo library, please let me know.

Also, please let me know if this worked for you 🙂

Learning & sharing

This is where we store some of our Web Development thoughts, tips and tricks for both our own uses and for others to learn from. Sharing is caring.

Please feel free to contribute to our blog posts; perhaps even teach us a few tricks of your own. We'd love to hear your thoughts.



X