Home > Backend Development > PHP Tutorial > PHP detects file type common code class (zip, rar, etc.) through file header_PHP tutorial

PHP detects file type common code class (zip, rar, etc.) through file header_PHP tutorial

WBOY
Release: 2016-07-21 15:34:05
Original
1068 people have browsed it

Sometimes we do this imperfectly. Some people may save some files on their computer, but they change the extension so that it falls within our file type. It cannot be displayed during actual access (because the extension does not match the file content). The following php class may be able to help us.
1. PHP detection class
First of all, let me explain that the above mapping relationship between file headers and file types comes from the Internet. If you have new files to check, you only need to add the mapping. If you need to know the file header information, you can open the standard file search through the tool: winhex. For example:
Code:

Copy code The code is as follows:

/*via file Name, get file type*
*@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();
}
/**
*Process file type mapping table*
*
* @param string $filename file type
* @return string file type, not found return: 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); // Read only 15 bytes. Different file types have different header information.
fclose($file);
$typelist=self::$_TypeList;
foreach ($typelist as $v)
{
$blen=strlen(pack("H*" ,$v[0])); //Get the number of bytes marked in the file header
$tbin=substr($bin,0,intval($blen)); ///Need to compare the file header length
if (strtolower($v[0])==strtolower(array_shift(unpack("H*",$tbin))))
{
return $v[1];
}
}
return $filetype;
}
/**
*Get file header and file type mapping table*
*
* @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);
}
}

How to get the header bytecode:
PHP detects file type common code class (zip, rar, etc.) through file header_PHP tutorial
You can see: png file, the header is 4 bytes (you need to check the relevant information to determine how many bytes the header mark is), the corresponding is: 89504E47
If you are not very familiar with PHP's pack unpack, you can check out:
How to use php park, unpark, ord functions (binary stream interface application example)

Calling example:
Copy code The code is as follows:

$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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322512.htmlTechArticleSometimes what we do is not perfect. Some people may save some files on their computer, but they change the extension so that they fall within our file type. It cannot be displayed during the actual visit (...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template