不通过扩展名,去判断文件类型
1 第一种方法 2 3 通过php的finfo_file() 4 5 $handle=finfo_open(FILEINFO_MIME_TYPE);//This function opens a magic database and returns its resource. 6 $fileInfo=finfo_file($handle,'./test.txt');// Return information about a file 7 finfo_close($handle); 8 print_r($fileInfo); 9 echo '==========="\n"'; 10 11 另外 12 finfo_buffer: Return information about a string buffer 13 finfo_close: Close fileinfo resource 14 mime_content_type:Detect MIME Content-type for a file (deprecated) 15 16 第二种方法 17 18 通过二进制流获取文件内容 19 20 $fp=fopen('test.txt','r')///实际是image/png 21 $bin = fread($fp, 2); //只读2字节 22 fclose($fp); 23 $str_info = @unpack(“C2chars”, $bin);//Unpack data from binary string 24 $type_code = intval($str_info['chars1'].$str_info['chars2']);// Get the integer value of a variable 25 $file_type = ”; 26 switch ($type_code) { 27 case 7790: 28 $file_type = ‘exe’; 29 break; 30 case 7784: 31 $file_type = ‘midi’; 32 break; 33 case 8075: 34 $file_type = ‘zip’; 35 break; 36 case 8297: 37 $file_type = ‘rar’; 38 break; 39 case 255216: 40 $file_type = ‘jpg’; 41 break; 42 case 7173: 43 $file_type = ‘gif’; 44 break; 45 case 6677: 46 $file_type = ‘bmp’; 47 break; 48 case 13780: 49 $file_type = ‘png’; 50 break; 51 default: 52 $file_type = ‘unknown’; 53 break; 54 }