Php accurately obtains the object MIME type. _ PHP Tutorial

Source: Internet
Author: User
Php accurately obtains the object MIME type ,. Php accurately obtains the object MIME type. This article describes how php accurately obtains the object MIME type. Share it with you for your reference. The specific implementation method is as follows: php $ mime php accurately obtains the object MIME type,

This example describes how php can accurately obtain the object MIME type. 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.

Examples in this article describes how php can accurately obtain the object MIME type. Share it with you for your reference. The specific implementation method is as follows: php $ mime =...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.