This article mainly introduces how PHP obtains the file MIME type, the interest friend's reference, hoped to be helpful to everybody.
The example in this article describes how PHP gets the file MIME type exactly. The implementation method is as follows:
<?php$mime = Array (//applications ' ai ' = ' application/postscript ', ' eps ' = ' application/postscript ', ' exe ' = ' application/octet-stream ', ' doc ' = ' application/vnd.ms-word ', ' xls ' and ' = ' application/vnd . Ms-excel ', ' ppt ' = ' application/vnd.ms-powerpoint ', ' pps ' = ' application/vnd.ms-powerpoint ', ' PDF ' =& Gt ' Application/pdf ', ' xml ' = ' application/xml ', ' odt ' = ' application/vnd.oasis.opendocument.text ', ' SWF ' = ' Application/x-shockwave-flash ',//Archives ' gz ' = ' application/x-gzip ', ' tgz ' = ' application/x ' -gzip ', ' bz ' = ' application/x-bzip2 ', ' bz2 ' = ' application/x-bzip2 ', ' tbz ' and ' application/x-bzip2 ' , ' zip ' = ' application/zip ', ' rar ' = ' application/x-rar ', ' tar ' = ' application/x-tar ', ' 7z ' =& Gt ' application/x-7z-compressed ',//texts ' txt ' = ' text/plain ', ' php ' = ' text/x-php ', ' html ' = ' te ' Xt/html ', ' HTM ' = ' text/html ', ' js ' = ' text/javascript ', ' css ' = ' text/css ', ' rtf ' = ' text/rtf ', ' rtfd ' = ' text/rtfd ', ' py ' = ' Text/x-python ', ' java ' = ' text/x-java-source ', ' 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 ' = ' audio/mpeg ', ' mid ' = ' Audio/midi ', ' ogg ' = ' Audi O/ogg ', ' mp4a ' = ' audio/mp4 ', ' wav ' = ' audio/wav ', ' wma ' = ' audio/x-ms-wma ',//video ' avi ' = ' Video/x-msvideo ', ' dv ' = ' video/x-dv ', ' mp4 ' = ' 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); This paragraph needs to be added, because if you use the Mime_content_type function to get a nonexistent $path, you 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.txtvar_dump (_getmimetype ($path));/*end of php*/
Summary: The above is the entire content of this article, I hope to be able to help you learn.
Related recommendations:
PHP Usage History Feature
Methods of PHP Process control and mathematical operations
Problems related to PHP garbage collection mechanism