Php obtains the file mime type program code

Source: Internet
Author: User
Tags rar zip

Mime_content_type: the MIME type of the specified object is returned,

Usage:

The code is as follows: Copy code
Echo mime_content_type ('php.gif '). "n ";
Echo mime_content_type ('test. Php ');

Output:

Image/gif
Text/plain

However, this function will not be available after php5.3.0. If you are using php5.3.0 or later, we can use

The code is as follows: Copy code

$ Finfo = finfo_open (FILEINFO_MIME );
$ Mimetype = finfo_file ($ finfo, $ filename );
Finfo_close ($ finfo );

To operate

The following describes the user-defined version of php.

The code is as follows: Copy code

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. Text ',
'Swf '=> 'application/x-shockwave-Flash ',
// Archives
'Gz '=> 'application/x-gzip ',
'Tgz' => 'application/x-gzip ',
'Yz' => '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 ',
'Html' => 'text/html ',
'Htm '=> 'text/html ',
'Js' => 'text/javascript ',
'Css '=> 'text/css ',
'Rtf '=> 'text/rtf ',
'Rtfs' => 'text/rtfs ',
'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 ',
'Ogc' => 'audio/ogc ',
'Mp4a '=> 'audio/mp4 ',
'Wav '=> 'audio/wav ',
'Wma' => 'audio/x-ms-wma ',
// Video
'Av' => '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 section needs to be added, because if the mime_content_type function is used to obtain a nonexistent $ path, 'application/octet-stream' will be returned'
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 '; // the current cursor does not exist in 1.txt
Var_dump (_ getMimeType ($ path ));

/* End of php */

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.