Php exact method to get file MIME type, _php tutorial

Source: Internet
Author: User

PHP exactly gets the file MIME type method,


The example in this article describes how PHP gets the file MIME type exactly. Share to everyone for your reference. The implementation method is as follows:

<?php$mime = Array (//applications ' ai ' = ' application/postscript ', ' eps ' = ' Application/postscrip T ', ' exe ' = ' application/octet-stream ', ' doc ' = ' application/vnd.ms-word ', ' xls ' = ' application/vn ' D.ms-excel ', ' ppt ' = ' application/vnd.ms-powerpoint ', ' pps ' = ' application/vnd.ms-powerpoint ', ' pdf ' = > ' application/pdf ', ' xml ' = ' application/xml ', ' odt ' = ' application/vnd.oasis.opendocument.text ', ' swf ' = ' application/x-shockwave-flash ',//Archives ' gz ' = ' application/x-gzip ', ' tgz ' = ' applicat ' Ion/x-gzip ', ' bz ' = ' application/x-bzip2 ', ' bz2 ' = ' application/x-bzip2 ', ' tbz ' and ' application/x-b ' Zip2 ', ' zip ' = ' application/zip ', ' rar ' = ' application/x-rar ', ' tar ' = ' application/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-source ', ' rb ' = ' Text/x-ruby ', ' sh ' = ' text/x-shellscript ', ' pl ' = ' text/x-perl ', ' sql ' = ' text/x-sql ',//images ' BMP ' =&G T  ' 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*/

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/1018524.html www.bkjia.com true http://www.bkjia.com/PHPjc/1018524.html techarticle PHP accurately obtains the file MIME type method, this article explains the PHP accurate obtains the file MIME type the method. Share to everyone for your reference. The implementation method is as follows: Php$mime = ...

  • 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.