Php accurately obtains the object MIME type

Source: Internet
Author: User

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:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

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 */

I hope this article will help you with php programming.

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.