PHP detects file types by file header generic code classes (zip rar etc.)

  • 2020-03-31 21:21:43
  • OfStack

Sometimes we don't do it perfectly. Some people may save some files on it, but they change the extension to be within our file type. It cannot be displayed during actual access (because the extension does not match the contents of the file). The following PHP class might help.
I. PHP detection class
First of all, the above file header and file type mapping relationship from the Internet, if you have a new file to check, just need to add the mapping. If you need to know the file header information, you can use the tool: winhex open the standard file to find. Such as:
Code:
 
<?php 
/* Get the file type by the file name * 
*@author chengmo* 
*@copyright cnblog.com/chengmo 2010-10-17 
*@version 0.1 
*$filename="d:/1.png";echo cFileTypeCheck::getFileType($filename);  Print: png 
*/ 
class cFileTypeCheck 
{ 
private static $_TypeList=array(); 
private static $CheckClass=null; 
private function __construct($filename) 
{ 
self::$_TypeList=$this->getTypeList(); 
} 
 
private function _getFileType($filename) 
{ 
$filetype="other"; 
if(!file_exists($filename)) throw new Exception("no found file!"); 
$file = @fopen($filename,"rb"); 
if(!$file) throw new Exception("file refuse!"); 
$bin = fread($file, 15); //Read only 15 bytes each of the different file types, header information is different.
fclose($file); 
$typelist=self::$_TypeList; 
foreach ($typelist as $v) 
{ 
$blen=strlen(pack("H*",$v[0])); //Gets the number of bytes of file header markup
$tbin=substr($bin,0,intval($blen)); /// need to compare file header length
if(strtolower($v[0])==strtolower(array_shift(unpack("H*",$tbin)))) 
{ 
return $v[1]; 
} 
} 
return $filetype; 
} 
 
public function getTypeList() 
{ 
return array(array("FFD8FFE1","jpg"), 
array("89504E47","png"), 
array("47494638","gif"), 
array("49492A00","tif"), 
array("424D","bmp"), 
array("41433130","dwg"), 
array("38425053","psd"), 
array("7B5C727466","rtf"), 
array("3C3F786D6C","xml"), 
array("68746D6C3E","html"), 
array("44656C69766572792D646174","eml"), 
array("CFAD12FEC5FD746F","dbx"), 
array("2142444E","pst"), 
array("D0CF11E0","xls/doc"), 
array("5374616E64617264204A","mdb"), 
array("FF575043","wpd"), 
array("252150532D41646F6265","eps/ps"), 
array("255044462D312E","pdf"), 
array("E3828596","pwl"), 
array("504B0304","zip"), 
array("52617221","rar"), 
array("57415645","wav"), 
array("41564920","avi"), 
array("2E7261FD","ram"), 
array("2E524D46","rm"), 
array("000001BA","mpg"), 
array("000001B3","mpg"), 
array("6D6F6F76","mov"), 
array("3026B2758E66CF11","asf"), 
array("4D546864","mid")); 
} 
public static function getFileType($filename) 
{ 
if(!self::$CheckClass) self::$CheckClass=new self($filename); 
$class=self::$CheckClass; 
return $class->_getFileType($filename); 
} 
} 

How to get header bytecode:
< img border = 0 SRC = "http://files.jb51.net/upload/201010/20101019023853183.gif" border = 0 >
You can see: PNG file, the header is 4 bytes (the header is how many bytes need to check the relevant information to determine), the corresponding is: 89504E47
If you're not familiar with PHP's pack unpack, check out:
PHP park, unpark, ord function usage method (binary stream interface application example)

Call instance:
 
$filename="d:/1.png"; 
echo $filename,"t",cFileTypeCheck::getFileType($filename),"rn"; 
$filename="d:/test.doc"; 
echo $filename,"t",cFileTypeCheck::getFileType($filename),"rn"; 
d:/1.png png 
d:/test.doc xls/doc 

Related articles: