Sometimes this is not perfect. Some people may store some files, but they modify the extension to make it 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 help us.
I. php Detection
First, the ing between the above file header and file type comes from the Internet. If you have a new file to be checked, you only need to add the ing. If you need to know the file header information, you can use the tool winhex to open the standard file search. For example:
Code:
Copy codeThe Code is as follows:
<? Php
/* Obtain the file type by 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 ();
}
/**
* Process the file type ing table *
*
* @ Param string $ filename file type
* @ Return string file type. The returned value is 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 for different file types and different header information.
Fclose ($ file );
$ Typelist = self ::$ _ TypeList;
Foreach ($ typelist as $ v)
{
$ Blen = strlen (pack ("H *", $ v [0]); // get the number of bytes marked by the file header
$ Tbin = substr ($ bin, 0, intval ($ blen); // compare the file header length
If (strtolower ($ v [0]) = strtolower (array_shift (unpack ("H *", $ tbin ))))
{
Return $ v [1];
}
}
Return $ filetype;
}
/**
* Get the file header and file type ing table *
*
* @ Return array ('key', value )...)
*/
Public function getTypeList ()
{
Return array ("FFD8FFE1", "jpg "),
Array ("89504E47", "png "),
Array ("47494638", "gif "),
Array ("49347a00", "tif "),
Array ("mongod", "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 ("5374616e64617213204a", "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 ("2e00001fd", "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:
We can see that the header of the png file is 4 bytes (the number of bytes marked by the header needs to be determined by yourself), corresponding to: 89504E47
If you are not familiar with the pack unpack of php, you can view:
Php park, unpark, ord function usage (binary stream interface application instance)
Call instance:
Copy codeThe Code is as follows:
$ 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