How do I find the MIME type of a file with PHP?

Viewed 112174

I have an index.php file which has to process many different file types. How do I guess the filetype based on the REQUEST_URI?

If I request http://site/image.jpg, and all requests redirect through index.php, which looks like this

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

How would I make that work correctly?

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

13 Answers

If you are sure you're only ever working with images, you can check out the exif_imagetype() PHP function, which attempts to return the image MIME type.

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.

Lastly, you can check out the mime_content_type() function - but it has been deprecated for the Fileinfo PECL extension.

mime_content_type() is deprecated, so you won't be able to count on it working in the future. There is a "fileinfo" PECL extension, but I haven't heard good things about it.

If you are running on a Unix-like server, you can do the following, which has worked fine for me:

$file = escapeshellarg($filename);
$mime = shell_exec("file -bi " . $file);
$filename should probably include the absolute path.

According to the PHP manual, the finfo-file function is 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 outdated mime_content_type function.

You can use finfo to accomplish this as of 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 get a more verbose string for some files; (apparently some image types will return size and colour depth information). Using the FILEINFO_MIME flag returns the mime-type and encoding if available (e.g. image/png; charset=binary or text/x-php; charset=us-ascii). See this site for more info.

I haven't used it, but there's a PECL extension for getting a file's MIME type. The official documentation for it is in the manual.

Depending on your purpose, a file extension can be ok, but it's not incredibly reliable since it's so easily changed.

If you're only dealing with images, you can use the [getimagesize()][1] function which contains all sorts of information about the image, including the type.

A more general approach would be to use the FileInfo extension from PECL.

Some people have serious complaints about that extension... so if you run into serious issues or cannot install the extension for some reason you might want to check out the deprecated function mime_content_type().

The MIME type of any file on your server can be gotten with 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');
Related