Php: how to accurately obtain the object MIME type _ PHP Tutorial

Source: Internet
Author: User
Php accurately obtains the object MIME type. Php: how to accurately obtain the object MIME type. This document describes how to accurately obtain the object MIME type by php. Share it with you for your reference. The specific implementation method is as follows :? 123456 php method for obtaining the object MIME type accurately

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

Bytes

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

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 :? 1 2 3 4 5 6...

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.