How to find mime file type with php?

So, I have an index.php file that processes many different types of files. how can I determine the file type based on REQUEST_URI .

If I request http://site/image.jpg and all requests are redirected via index.php, which looks like

 <?php include('/www/site'.$_SERVER['REQUEST_URI']); ?> 

How can I do this job right?

Should I test based on the extension of the requested file, or is there a way to get the file type?

+47
content-type filesystems php
Sep 25 '08 at 17:51
source share
13 answers

If you are sure that you work only with images, you can check getimagesize () exif_imagetype () A PHP function that attempts to return an mime type image.

If you don't mind external dependencies, you can also check out the excellent getID3 library, which can determine the mime type of many different file types.

Finally, you can check the mime_content_type () function, but it is deprecated for the Fileinfo PECL extension.

+48
Sep 25 '08 at 17:59
source share

mime_content_type () is deprecated, so you won’t be able to expect it to work in the future. There is the "fileinfo" PECL extension, but I have not heard anything about it.

If you are running on a * nix server, you can do the following, which worked fine for me:

 $file = escapeshellarg( $filename ); $mime = shell_exec("file -bi " . $file); $filename should probably include the absolute path. 
+22
Sep 25 '08 at 18:05
source share
 function get_mime($file) { if (function_exists("finfo_file")) { $finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension $mime = finfo_file($finfo, $file); finfo_close($finfo); return $mime; } else if (function_exists("mime_content_type")) { return mime_content_type($file); } else if (!stristr(ini_get("disable_functions"), "shell_exec")) { // http://stackoverflow.com/a/134930/1593459 $file = escapeshellarg($file); $mime = shell_exec("file -bi " . $file); return $mime; } else { return false; } } 

None of this works for me - mime_content_type deprecated, finfo not installed, and shell_exec not allowed.

+17
Aug 30 '12 at 7:29
source share

I'm really tired of missing the standard MIME sniffing methods in PHP. Install fileinfo ... Use outdated functions ... Oh, these work, but only for images! I was tired of this, so I did some research and found the WHATWG Mimesniffing spec - I think it's still a draft.

In any case, using this specification, I was able to implement mimesniffer in PHP. Performance is not a problem. In fact, on my humble machine, I was able to open and sniff thousands of files before the PHP timeout.

Here is the MimeReader class .

 require_once("MimeReader.php"); $mime = new MimeReader(<YOUR FILE PATH>); $mime_type_string = $mime->getType(); // "image/jpeg" etc. 
+10
Aug 01 '13 at 2:39
source share

If you only work with images and you need a mime type (e.g. for headings), this is the fastest and most direct answer:

 $file = 'path/to/image.jpg'; $image_mime = image_type_to_mime_type(exif_imagetype($file)); 

It will display the true mime type of the image, even if you rename your image file

+8
Mar 06 '13 at 19:34
source share

According to the php manual, the finfo-file function is the best way to do this. However, you will need to install the FileInfo PECL extension.

If the extension is not an option, you can use the deprecated mime_content_type function.

+1
Sep 25 '08 at 18:01
source share

mime_content_type() seems to be a way to go, despite the comments above, saying it is deprecated. This is not the case - or at least this incarnation of mime_content_type() not deprecated, according to http://php.net/manual/en/function.mime-content-type.php . It is part of the FileInfo extension, but the PHP documentation now states that it is enabled by default with PHP 5.3.0.

+1
Jan 31 '16 at 5:20
source share

You can use finfo to accomplish this with PHP 5.3:

 <?php $info = new finfo(FILEINFO_MIME_TYPE); echo $info->file('myImage.jpg'); // prints "image/jpeg" 

The FILEINFO_MIME_TYPE flag is optional; without it, you will get a more detailed line for some files; (apparently, some types of images return information about the size and color depth). Using the FILEINFO_MIME flag returns the mime type and encoding, if available (for example, image / png; charset = binary or text / x-php; charset = us-ascii). See this site for more information.

+1
12 Oct '16 at 17:34
source share

I did not use it, but there is a PECL extension to get the file type. The official documentation for it is in the manual .

Depending on your goals, the file extension may be fine, but it is not incredibly reliable, as it has changed so easily.

0
Sep 25 '08 at 18:00
source share

If you use only images, you can use the [getimagesize()][1] function, which contains all kinds of image information, including type.

A more general approach is to use the FileInfo extension from PECL. The PHP documentation for this extension can be found at: http://us.php.net/manual/en/ref.fileinfo.php

Some people have serious complaints about this extension ... so if you have serious problems or you can’t install the extension for some reason, you can check the obsolete mime_content_type() function

0
Sep 25 '08 at 18:03
source share

I got very good results using the user function from http://php.net/manual/de/function.mime-content-type.php @ '' john dot howard at prismmg dot com 26-Oct-2009 03:43 ' ''

 function get_mime_type($filename, $mimePath = '../etc') { ... 

which does not use finfo, exec function or deprecated function

works well with remote resources!

0
Sep 12
source share

If you start Linux and you have an extension, you can simply read the MIME type from /etc/mime.types, creating a hash array. Then you can store this in memory and just call MIME using the array key :)

 /** * Helper function to extract all mime types from the default Linux /etc/mime.types */ function get_mime_types() { $mime_types = array(); if ( file_exists('/etc/mime.types') && ($fh = fopen('/etc/mime.types', 'r')) !== false ) { while (($line = fgets($fh)) !== false) { if (!trim($line) || substr($line, 0, 1) === '#') continue; $mime_type = preg_split('/\t+/', rtrim($line)); if ( is_array($mime_type) && isset($mime_type[0]) && $mime_type[0] && isset($mime_type[1]) && $mime_type[1] ) { foreach (explode(' ', $mime_type[1]) as $ext) { $mime_types[$ext] = $mime_type[0]; } } } fclose($fh); } return $mime_types; } 
0
Aug 24 '16 at 18:58
source share

Mime any file on your server can be obtained using this

 <?php function get_mime($file_path){ $finfo = new finfo(FILEINFO_MIME_TYPE); $type = $finfo->file(file_path); } $mime = get_mime('path/to/file.ext'); 
0
Nov 28 '17 at 1:02
source share



All Articles