Home >php教程 >php手册 >php通过文件头检测文件类型通用代码类(zip,rar等)

php通过文件头检测文件类型通用代码类(zip,rar等)

WBOY
WBOYOriginal
2016-06-21 08:56:171234browse

有时候我们这样做还不完善。可能有些人上存一些文件,但是他通过修改扩展名,让在我们的文件类型之内。 单实际访问时候又不能展示(因为扩展名与文件内容不符)。下面这个php类,可能能够给我们带来帮助。
一、php检测类
首先说明下,上面文件头与文件类型映射关系来自网上,如果你有新的文件需要检查,只需要将映射加入即可。 如果你需要知道文件头信息,可以通过工具:winhex打开标准文件查找。如:
代码:

复制代码 代码如下:


/*通过文件名,获得文件类型*
*@author chengmo*
*@copyright cnblog.com/chengmo 2010-10-17
*@version 0.1
*$filename="d:/1.png";echo cFileTypeCheck::getFileType($filename); 打印:png
*/
class cFileTypeCheck
{
private static $_TypeList=array();
private static $CheckClass=null;
private function __construct($filename)
{
self::$_TypeList=$this->getTypeList();
}
/**
*处理文件类型映射关系表*
*
* @param string $filename 文件类型
* @return string 文件类型,没有找到返回:other
*/
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); //只读15字节 各个不同文件类型,头信息不一样。
fclose($file);
$typelist=self::$_TypeList;
foreach ($typelist as $v)
{
$blen=strlen(pack("H*",$v[0])); //得到文件头标记字节数
$tbin=substr($bin,0,intval($blen)); ///需要比较文件头长度
if(strtolower($v[0])==strtolower(array_shift(unpack("H*",$tbin))))
{
return $v[1];
}
}
return $filetype;
}
/**
*得到文件头与文件类型映射表*
*
* @return array array(array("key",value)...)
*/
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);
}
}


怎么样获得头字节码:

可以看到:png 文件,头是4个字节(头标记是多少字节需要自己查相关资料确定),对应是:89504E47
如果你对php的pack unpack 不很熟悉,可以查看:
php park、unpark、ord 函数使用方法(二进制流接口应用实例)

调用实例:

复制代码 代码如下:


$filename="d:/1.png";
echo $filename,"\t",cFileTypeCheck::getFileType($filename),"\r\n";
$filename="d:/test.doc";
echo $filename,"\t",cFileTypeCheck::getFileType($filename),"\r\n";
d:/1.png png
d:/test.doc xls/doc



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn