Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine the extension(s) associated with a MIME type in PHP?

Tags:

Is there a quick and dirty mapping of MIME types to extensions in PHP that I can make use of?

like image 1000
Alexander Trauzzi Avatar asked Jul 18 '09 16:07

Alexander Trauzzi


People also ask

How do I get MIME type extension?

string mimeType = MimeMapping. GetMimeMapping(fileName); If you need to add custom mappings you probably can use reflection to add mappings to the BCL MimeMapping class, it uses a custom dictionary that exposes this method, so you should invoke the following to add mappings (never tested tho, but should prob. work).

What is the extension of MIME file?

MIME files are more commonly seen using a . MIM extension.

What is the PHP function to check the MIME type of a file?

The mime_content_type() function is an inbuilt function in PHP which is used to get the MIME content-type of a file. Parameters: This function accepts single parameter $file which specifies the path of the file which MIME details to be find.

How do I know my MIME type?

For detecting MIME-types, use the aptly named "mimetype" command. It has a number of options for formatting the output, it even has an option for backward compatibility to "file". But most of all, it accepts input not only as file, but also via stdin/pipe, so you can avoid temporary files when processing streams.


2 Answers

Not built-in, but it's not terribly hard to roll your own:

function system_extension_mime_types() {     # Returns the system MIME type mapping of extensions to MIME types, as defined in /etc/mime.types.     $out = array();     $file = fopen('/etc/mime.types', 'r');     while(($line = fgets($file)) !== false) {         $line = trim(preg_replace('/#.*/', '', $line));         if(!$line)             continue;         $parts = preg_split('/\s+/', $line);         if(count($parts) == 1)             continue;         $type = array_shift($parts);         foreach($parts as $part)             $out[$part] = $type;     }     fclose($file);     return $out; }  function system_extension_mime_type($file) {     # Returns the system MIME type (as defined in /etc/mime.types) for the filename specified.     #     # $file - the filename to examine     static $types;     if(!isset($types))         $types = system_extension_mime_types();     $ext = pathinfo($file, PATHINFO_EXTENSION);     if(!$ext)         $ext = $file;     $ext = strtolower($ext);     return isset($types[$ext]) ? $types[$ext] : null; }  function system_mime_type_extensions() {     # Returns the system MIME type mapping of MIME types to extensions, as defined in /etc/mime.types (considering the first     # extension listed to be canonical).     $out = array();     $file = fopen('/etc/mime.types', 'r');     while(($line = fgets($file)) !== false) {         $line = trim(preg_replace('/#.*/', '', $line));         if(!$line)             continue;         $parts = preg_split('/\s+/', $line);         if(count($parts) == 1)             continue;         $type = array_shift($parts);         if(!isset($out[$type]))             $out[$type] = array_shift($parts);     }     fclose($file);     return $out; }  function system_mime_type_extension($type) {     # Returns the canonical file extension for the MIME type specified, as defined in /etc/mime.types (considering the first     # extension listed to be canonical).     #     # $type - the MIME type     static $exts;     if(!isset($exts))         $exts = system_mime_type_extensions();     return isset($exts[$type]) ? $exts[$type] : null; } 
like image 105
chaos Avatar answered Sep 18 '22 12:09

chaos


You could use mime_content_type but it is deprecated. Use fileinfo instead:

function getMimeType($filename) {     $finfo = finfo_open(FILEINFO_MIME_TYPE);     $mime = finfo_file($finfo, $filename);     finfo_close($finfo);     return $mime; } 
like image 43
Jorge Barata Avatar answered Sep 20 '22 12:09

Jorge Barata