The examples in this article describe how PHP accurately obtains file MIME types. Share to everyone for your reference. The implementation method is as follows:
<?php $mime = Array (//applications ' ai ' => ' application/postscript ', ' eps ' => ' application/post Script ', ' exe ' => ' application/octet-stream ', ' Doc ' => ' application/vnd.ms-word ', ' xls ' => ' applic
Ation/vnd.ms-excel ', ' ppt ' => ' application/vnd.ms-powerpoint ', ' pps ' => ' Application/vnd.ms-powerpoint ', ' PDF ' => ' application/pdf ', ' xml ' => ' Application/xml ', ' odt ' => ' T.text ', ' swf ' => ' application/x-shockwave-flash ',//Archives ' GZ ' => ' application/x-gzip ', ' tgz ' => ' Application/x-gzip ', ' BZ ' => ' application/x-bzip2 ', ' bz2 ' => ' application/x-bzip2 ', ' tbz ' =& Gt ' Application/x-bzip2 ', ' Zip ' => ' application/zip ', ' rar ' => ' application/x-rar ', ' tar ' => ' Applica Tion/x-tar ', ' 7z ' => ' application/x-7z-compressed ',//texts ' txt ' => ' text/plain ', ' php ' => ' Text/x-php ', ' HTML ' => ' text/html ', ' htm ' => ' text/html ', ' js ' => ' text/javascript ', ' css ' => ' text/css ', ' RTF ' => ' text/rtf ', ' rtfd ' => ' text/rtfd ', ' py ' => ' Text/x-python ', ' java ' => ' TEXT/X-JAVA-SOURC E ', ' RB ' => ' text/x-ruby ', ' sh ' => ' text/x-shellscript ', ' pl ' => ' text/x-perl ', ' SQL ' => ' Text/x-sql ',//images ' bmp ' => ' image/x-ms-bmp ', ' jpg ' => ' image/jpeg ', ' jpeg ' => ' image/jpeg ' , ' gif ' => ' image/gif ', ' png ' => ' image/png ', ' tif ' => ' Image/tiff ', ' TIFF ' => ' Image/tiff ' , ' TGA ' => ' image/x-targa ', ' psd ' => ' image/vnd.adobe.photoshop '//audio ' mp3 ' ', ' mid ' => ' Audio/midi ', ' ogg ' => ' Audio/ogg ', ' mp4a ' => ' audio/mp4 ', ' wav ' => ' audio/wav ' , ' wma ' => ' audio/x-ms-wma ',//Video ' avi ' => ' video/x-msvideo ', ' dv ' => ' VIDEO/X-DV ', ' m
P4 ' => ' Video/mp4 ', ' MPEG ' => ' video/mpeg ', ' mpg ' => ' video/mpeg ', ' mov ' => ' video/quicktime ', ' wm ' => ' video/x
-ms-wmv ', ' flv ' => ' video/x-flv ', ' mkv ' => ' Video/x-matroska ');
function _getmimedetect () {if (class_exists (' Finfo ')) {return ' finfo ';
else if (function_exists (' Mime_content_type ')) {return ' Mime_content_type ';
else if (function_exists (' exec ')) {$result = exec (' File-ib '. Escapeshellarg (__file__));
if (0 = = Strpos ($result, ' text/x-php ') OR 0 = = Strpos ($result, ' text/x-c++ ')) {return ' Linux ';
$result = EXEC (' File-ib '. Escapeshellarg (__file__));
if (0 = = Strpos ($result, ' text/x-php ') OR 0 = = Strpos ($result, ' text/x-c++ ')) {return ' BSD ';
} return ' internal ';
function _getmimetype ($path) {global $mime;
$fmime = _getmimedetect ();
Switch ($fmime) {case ' finfo ': $finfo = Finfo_open (fileinfo_mime); if ($finfo) $type = @finfo_file ($finfo, $path);
Break
Case ' Mime_content_type ': $type = Mime_content_type ($path);
Break
Case ' Linux ': $type = exec (' File-ib '. Escapeshellarg ($path));
Break
Case ' BSD ': $type = exec (' File-ib '. Escapeshellarg ($path));
Break
Default: $pinfo = PathInfo ($path); $ext = Isset ($pinfo [' extension '])?
Strtolower ($pinfo [' extension ']): '; $type = Isset ($mime [$ext])?
$mime [$ext]: ' Unkown ';
Break
$type = explode ('; ', $type); You need to add this because if you use the Mime_content_type function to get a nonexistent $path, you will return ' Application/octet-stream ' if ($fmime!= ' internal ' and $type [0]
= = ' Application/octet-stream ') {$pinfo = PathInfo ($path); $ext = Isset ($pinfo [' extension '])?
Strtolower ($pinfo [' extension ']): ';
if (!empty ($ext) and!empty ($mime [$ext])) {$type [0] = $mime [$ext];
} return $type [0]; $path = ' 1.txt ';
In fact, the current path does not exist 1. txt var_dump (_getmimetype ($path)); /*end of php*/
I hope this article will help you with your PHP programming.