Sometimes it's not perfect. Some people may have saved some files, but he has changed the extension so that it is within our file type. The actual access is not displayed (because the extension does not match the contents of the file). Here's a PHP class that might be helpful.
first, the PHP test class
First of all, the above file header and file type mapping relationship from the Internet, if you have a new file need to check, just need to add the map. If you need to know the header information, you can use the tool: Winhex to open the standard file lookup. Such as:
Code:
Copy Code code as follows:
<?php
/* Get 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); Printing: PNG
*/
Class Cfiletypecheck
{
private static $_typelist=array ();
private static $CheckClass =null;
Private function __construct ($filename)
{
self::$_typelist= $this->gettypelist ();
}
/**
* Process file Type mapping Relationship table *
*
* @param string $filename file type
* @return string file type, no return found: 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 of different file types, header information is not the same.
Fclose ($file);
$typelist =self::$_typelist;
foreach ($typelist as $v)
{
$blen =strlen (Pack ("h*", $v [0])); Get file header tag byte number
$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;
}
/**
* 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 byte code:
Can see: PNG file, header is 4 bytes (header tag is how many bytes need to check the relevant data to determine), corresponding to: 89504E47
If you are not familiar with the PHP pack unpack, you can view:
PHP Park, Unpark, Ord function usage (binary flow interface application example)
Invoke instance:
Copy Code code 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