PHP detects the file type code through the file header .? Php * get the file type through the file name ** @ authorchengmo ** @ copyrightcnblog. comchengmo2010-10-19 * @ version0.1 * $ filenamed: 1.png; echocFileTypeCheck: getFileType ($ filena /* Obtain the file type by file name *
* @ Author chengmo *
* @ Copyright cnblog.com/chengmo 2010-10-19
* @ 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 );
}
}
?>
Http://www.bkjia.com/PHPjc/486084.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/486084.htmlTechArticle? Php/* get the file type ** @ author chengmo ** @ copyright cnblog.com/chengmo 2010-10-19 * @ version 0.1 * $ filename = "d:/1.png"; echo cFileTypeCheck :: getFileType ($ filena...