How PHP Gets the file MIME type

Source: Internet
Author: User
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 ' =&gt ;  ' 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

Related Article

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.