I have local access to files, which I need to get their MIME types. Working in WAMP/LAMP, CodeIgniter, and Zend libraries. What's the best way to get MIME type information?
Asked
Active
Viewed 3,959 times
5 Answers
5
There's no easy way. You could try:
http://www.php.net/manual/en/function.finfo-file.php
// return mime type ala mimetype extension
$finfo = finfo_open(FILEINFO_MIME_TYPE);
Of course, this assumes you can install PECL extensions.

David Titarenco
- 32,662
- 13
- 66
- 111
1
I think you need head. Quickest way is to do a head request, or in PHP under apache you can use apache_lookup_uri or in PHP 5.3 you can use FileInfo (I'd still recommend apache_lookup_uri or a simple head request though).

nathan
- 5,402
- 1
- 22
- 18
-
1`apache_lookup_uri` does not give you a MIME type, AFAIK. FileInfo will work, if you have PECL, as noted by @David. – Amadan Jul 07 '10 at 03:10
-
1apache_lookup_uri amongst other things returns 'content_type' which is the mime-type – nathan Jul 07 '10 at 11:33
-
2I renamed one of my .PHP files to .PNG and `apache_lookup_uri()` returned `image/png` as the Content-Type, which is proof that this is nothing more than an extension to "fake" mime type mapping. Furthermore, it might be dangerous since Apache actually requests the file and might lead to code execution (not sure about this). – Alix Axel Feb 17 '11 at 18:39
-
@Alix-Axel seems obvious enough, since apache doesn't have any sniffing in it and relies on mapping file extensions to media types. – nathan Apr 10 '11 at 06:32
1
Its never a good idea to try and find the mime type based on the file extension, as this can obviously be renamed by the used whos uploading - from .exe to .jpg
Real mime type detection is part of your overall security measures.

Christof Coetzee
- 139
- 1
- 2
0
Try CI's built in function "get_mime_by_extension($file)". You can find it in the "system/helpers/file_helper.php" file

Craig Myles
- 5,206
- 3
- 40
- 37