This article mainly introduces php's method for accurately obtaining the object MIME type, and involves php's related techniques for file attribute operations, for more information about how to obtain the MIME type of a file, see the example in this article. Share it with you for your reference. The specific implementation method is as follows:
Signature <? Php $ mime = array (// applications 'AI' => 'application/postscript ', 'eps' => 'application/postscript ', 'exe '=> 'application/octet-stream', 'Doc' => 'application/vnd. ms-word', 'XLS '=> 'application/vnd. ms-excel ', 'ppt' => 'application/vnd. ms-powerpoint ', 'pps' => 'application/vnd. ms-powerpoint ', 'PDF' => 'application/pdf ', 'xml' => 'application/XML', 'odt' => 'application/vnd. oasis. opendocument. tex T', 'swf '=> 'application/x-shockwave-Flash', // archives 'gz' => 'application/x-gzip ', 'tgz' => 'application/x-gzip ', 'BZ' => 'application/x-bzip2', 'bz2' => 'application/x-bzip2 ', 'tbz' => 'application/x-bzip2 ', 'Zip' => 'application/zip', 'rar '=> 'application/x-rar ', 'tar '=> 'application/x-tar', '7z' => 'application/x-7z-compressed ', // texts 'txt' => 'text/plain ', 'php' => 'text/x-php', 'htm L '=> '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-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', 'ogy' => 'audio/ogc ', 'mp4a '=> 'audio/mp4', 'wav' => 'audio/wav ', 'wm' => 'audio/x-ms-wm ', // video 'Av' => 'video/x-msvideo', 'dv' => 'video/x-DV', 'MP4' => 'video/mp4 ', 'MPEG '=> 'video/mpeg', 'mpg' => 'video/mpe G', '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_fi Le ($ 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 section, if the mime_content_type function is used to obtain a nonexistent $ path, '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 '; // 1.txt var_dump (_ getMimeType ($ path) does not exist in the current Shard on the primary node;/* End of php */
I hope this article will help you with php programming.