I've written a validation class here that uses the file header to determine the file format. (also not completely secure, if the user forged the file header, can also be verified)
Copy the Code code as follows:
/**
* Type of document to be checked
*
* @author Silver
*/
Class Filetypevalidation
{
File types, different header information
private static $_fileformats = Array (
' JP2 ' = ' 0000000c6a502020 ',
' 3gp ' = ' 0000002066747970 ',
' 3gp5 ' = ' 0000001866747970 ',
' M4A ' = ' 00000020667479704d3441 ',
' ico ' = ' 00000100 ',
' SPL ' = ' 00000100 ',
' VOB ' = ' 000001BA ',
' cur ' = ' 00000200 ',
' wb2 ' = ' 00000200 ',
' WK1 ' = ' 0000020006040600 ',
' WK3 ' = ' 00001a0000100400 ',
' Wk4 ' = ' 00001a0002100400 ',
' Wk5 ' = ' 00001a0002100400 ',
' 123 ' = ' 00001a00051004 ',
' Qxd ' = ' 00004d4d585052 ',
' mdf ' = ' 010f0000 ',
' TR1 ' = ' 0110 ',
' RGB ' = ' 01da01010003 ',
' DRW ' = ' 07 ',
' DSS ' = ' 02647373 ',
' Dat ' = ' a90d000000000000 ',
' Db3 ' = ' 03 ',
' Qph ' = ' 03000000 ',
' ADX ' = ' 80000020031204 ',
' DB4 ' = ' 04 ',
' N ' = ' FFFE0000 ',
' A ' = ' FFFE0000 ',
' SKF ' = ' 07534b46 ',
' DTD ' = ' 0764743264647464 ',
' db ' = ' d0cf11e0a1b11ae1 ',
' PCX ' = ' 0a050101 ',
' MP ' = ' 0CED ',
' Doc ' = ' d0cf11e0a1b11ae1 ',
' NRI ' = ' 0e4e65726f49534f ',
' wks ' = ' FF00020004040554 ',
' PF ' = ' 1100000053434341 ',
' ntf ' = ' 4E49544630 ',
' NSF ' = ' 4e45534d1a01 ',
' Arc ' = ' 41724301 ',
' Pak ' = ' 5041434B ',
' ETH ' = ' 1a350100 ',
' mkv ' = ' 1a45dfa393428288 ',
' ws ' = ' 1d7d ',
' GZ ' = ' 1f8b08 ',
' Tar.z ' = ' 1fa0 ',
' Ain ' = ' 2112 ',
' lib ' = ' 213c617263683e0a ',
' msi ' = ' d0cf11e0a1b11ae1 ',
' VMDK ' = ' 4b444d ',
' DSP ' = ' 23204d6963726f73 ',
' Amr ' = ' 2321414d52 ',
' HDR ' = ' 49536328 ',
' Sav ' = ' 24464c3240282329 ',
' EPS ' = ' c5d0d3c6 ',
' PDF ' = ' 25504446 ',
' FDF ' = ' 25504446 ',
' hqx ' = ' 2854686973206669 ',
' Log ' = ' 2a2a2a2020496e73 ',
' IVR ' = ' 2E524543 ',
' rm ' = ' 2e524d46 ',
' rmvb ' = ' 2e524d46 ',
' ra ' = ' 2e7261fd00 ',
' Au ' = ' 646e732e ',
' Cat ' = ' 30 ',
' evt ' = ' 300000004c664c65 ',
' asf ' = ' 3026b2758e66cf11 ',
' wma ' = ' 3026b2758e66cf11 ',
' wmv ' = ' 3026b2758e66cf11 ',
' WRI ' = ' be000000ab ',
' 7z ' = ' 377abcaf271c ',
' PSD ' = ' 38425053 ',
' SLE ' = ' 414376 ',
' ASX ' = ' 3C ',
' XDR ' = ' 3C ',
' DCI ' = ' 3c21646f63747970 ',
' Manifest ' = ' 3c3f786d6c2076657273696f6e3d ',
' xml ' = ' 3c3f786d6c2076657273696f6e3d22312e30223f3e ',
' msc ' = ' D0CF11E0A1B11AE1 ',
' fm ' = ' 3c4d616b65724669 ',
' MIF ' = ' 56657273696f6e20 ',
' gid ' = ' 4c4e0200 ',
' hlp ' = ' 4c4e0200 ',
' dwg ' = ' 41433130 ',
' Syw ' = ' 414d594f ',
' Abi ' = ' 414f4c494e444558 ',
' Aby ' = ' 414f4c4442 ',
' Bag ' = ' 414f4c2046656564 ',
' idx ' = ' 5000000020000000 ',
' Ind ' = ' 414f4c494458 ',
' PFC ' = ' 414f4c564d313030 ',
' org ' = ' 414f4c564d313030 ',
' VCF ' = ' 424547494e3a5643 ',
' Bin ' = ' 424c4932323351 ',
' BMP ' = ' 424D ',
' Dib ' = ' 424D ',
' PRC ' = ' 424f4f4b4d4f4249 ',
' bz2 ' = ' 425a68 ',
' tar.bz2 ' = ' 425a68 ',
' tbz2 ' = ' 425a68 ',
' TB2 ' = ' 425a68 ',
' RTD ' = ' 43232b44a4434da5 ',
' CBD ' = ' 434246494c45 ',
' ISO ' = ' 4344303031 ',
' CLB ' = ' 434f4d2b ',
' CPT ' = ' 43505446494c45 ',
' Cru ' = ' 43525553482076 ',
' swf ' = ' 465753 ',
' CTF ' = ' 436174616c6f6720 ',
' DMS ' = ' 444d5321 ',
' ADF ' = ' 5245564e554d3a2c ',
' DVR ' = ' 445644 ',
' Ifo ' = ' 445644 ',
' cdr ' = ' 52494646 ',
' VCD ' = ' 454E545259564344 ',
' MDI ' = ' 4550 ',
' E01 ' = ' 4c5646090d0aff00 ',
' evtx ' = ' 456c6646696c6500 ',
' Qbb ' = ' 458600000600 ',
' CPE ' = ' 464158434f564552 ',
' flv ' = ' 464c56 ',
' Aiff ' = ' 464f524d00 ',
' eml ' = ' 582D ',
' gif ' = ' 47494638 ',
' Pat ' = ' 47504154 ',
' gx2 ' = ' 475832 ',
' SH3 ' = ' 4848474231 ',
' tif ' = ' 4d4d002b ',
' TIFF ' = ' 4d4d002b ',
' mp3 ' = ' 494433 ',
' Koz ' = ' 49443303000000 ',
' CRW ' = ' 49491a0000004845 ',
' Cab ' = ' 4d534346 ',
' Lit ' = ' 49544f4c49544c53 ',
' Chi ' = ' 49545346 ',
' chm ' = ' 49545346 ',
' Jar ' = ' 5f27a889 ',
' JG ' = ' 4a47040e000000 ',
' KGB ' = ' 4b47425f61726368 ',
' shd ' = ' 68490000 ',
' lnk ' = ' 4c00000001140200 ',
' obj ' = ' 80 ',
' pdb ' = ' ACED000573720012 ',
' Mar ' = ' 4d41723000 ',
' DMP ' = ' 504147454455 ',
' Hdmp ' = ' 4d444d5093a7 ',
' MLS ' = ' 4d563243 ',
' MMF ' = ' 4d4d4d440000 ',
' NVRAM ' = ' 4d52564e ',
' Ppz ' = ' 4d534346 ',
' SNP ' = ' 4d534346 ',
' tlb ' = ' 4d53465402000100 ',
' DVF ' = ' 4d535f564f494345 ',
' msv ' = ' 4d535f564f494345 ',
' Mid ' = ' 4d546864 ',
' midi ' = ' 4d546864 ',
' DSN ' = ' 4D56 ',
' com ' = ' EB ',
' dll ' = ' 4d5a ',
' DRV ' = ' 4d5a ',
' exe ' = ' 4d5a ',
' pif ' = ' 4d5a ',
' Qts ' = ' 4d5a ',
' Qtx ' = ' 4d5a ',
' sys ' = ' FFFFFFFF ',
' ACM ' = ' 4d5a ',
' Ax ' = ' 4d5a900003000000 ',
' cpl ' = ' DCDC ',
' fon ' = ' 4d5a ',
' ocx ' = ' 4d5a ',
' OLB ' = ' 4d5a ',
' SCR ' = ' 4d5a ',
' VBX ' = ' 4d5a ',
' VxD ' = ' 4d5a ',
' 386 ' = ' 4d5a ',
' API ' = ' 4d5a900003000000 ',
' Flt ' = ' 76323030332E3130 ',
' Zap ' = ' 4d5a90000300000004000000ffff ',
' SLn ' = ' 4d6963726f736f66742056697375616c ',
' jnt ' = ' 4e422a00 ',
' JtP ' = ' 4e422a00 ',
' Cod ' = ' 4e616d653a20 ',
' DBF ' = ' 4f504c4461746162 ',
' OGA ' = ' 4f67675300020000 ',
' Ogg ' = ' 4f67675300020000 ',
' OGV ' = ' 4f67675300020000 ',
' OGX ' = ' 4f67675300020000 ',
' Dw4 ' = ' 4f7b ',
' PGM ' = ' 50350A ',
' pax ' = ' 504158 ',
' PGD ' = ' 504750644d41494e ',
' img ' = ' eb3c902a ',
' Zip ' = ' 504b0304140000 ',
' docx ' = ' 504b030414000600 ',
' pptx ' = ' 504b030414000600 ',
' xlsx ' = ' 504b030414000600 ',
' KWD ' = ' 504b0304 ',
' Odt ' = ' 504b0304 ',
' ODP ' = ' 504b0304 ',
' Ott ' = ' 504b0304 ',
' SxC ' = ' 504b0304 ',
' Sxd ' = ' 504b0304 ',
' SXi ' = ' 504b0304 ',
' Sxw ' = ' 504b0304 ',
' wmz ' = ' 504b0304 ',
' Xpi ' = ' 504b0304 ',
' xps ' = ' 504b0304 ',
' XPT ' = ' 5850434f4d0a5479 ',
' grp ' = ' 504d4343 ',
' Qemu ' = ' 514649 ',
' Abd ' = ' 5157205665722E20 ',
' QSD ' = ' 5157205665722E20 ',
' reg ' = ' FFFE ',
' Sud ' = ' 52454745444954 ',
' ani ' = ' 52494646 ',
' CMX ' = ' 52494646 ',
' Ds4 ' = ' 52494646 ',
' 4xm ' = ' 52494646 ',
' avi ' = ' 52494646 ',
' CDA ' = ' 52494646 ',
' QCP ' = ' 52494646 ',
' rmi ' = ' 52494646 ',
' wav ' = ' 52494646 ',
' Cap ' = ' 58435000 ',
' rar ' = ' 526172211a0700 ',
' AST ' = ' 5343486C ',
' SHW ' = ' 53484f57 ',
' CPI ' = ' ff464f4e54 ',
' Sit ' = ' 5374756666497420 ',
' SDR ' = ' 534d415254445257 ',
' Cnv ' = ' 53514c4f434f4e56 ',
' cal ' = ' b5a2b0b3b3b0a5b5 ',
' Info ' = ' E310000100000000 ',
' UCE ' = ' 55434558 ',
' Ufa ' = ' 554641c6d2c1 ',
' pch ' = ' 564350434830 ',
' ctl ' = ' 56455253494f4e20 ',
' Ws2 ' = ' 575332303030 ',
' LWP ' = ' 576f726450726f ',
' BDR ' = ' 5854 ',
' Zoo ' = ' 5a4f4f20 ',
' ECF ' = ' 5b47656e6572616c ',
' VCW ' = ' 5b4d535643 ',
' Dun ' = ' 5b50686f6e655d ',
' Sam ' = ' 5b7665725d ',
' CPX ' = ' 5b57696e646f7773 ',
' cfg ' = ' 5b666c7473696d2e ',
' cas ' = ' 5f434153455f ',
' CBK ' = ' 5f434153455f ',
' Arj ' = ' 60EA ',
' vhd ' = ' 636f6e6563746978 ',
' csh ' = ' 6375736800000002 ',
' P10 ' = ' 64000000 ',
' Dex ' = ' 6465780a30303900 ',
' DSW ' = ' 64737766696c65 ',
' FLAC ' = ' 664c614300000022 ',
' DBB ' = ' 6c33336c ',
' ACD ' = ' 72696666 ',
' Ram ' = ' 727473703a2f2f ',
' DMG ' = ' 78 ',
' LGC ' = ' 7b0d0a6f20 ',
' LGD ' = ' 7b0d0a6f20 ',
' PWI ' = ' 7b5c707769 ',
' rtf ' = ' 7b5c72746631 ',
' PSP ' = ' 7e424b00 ',
' wab ' = ' 9ccbcb8d1375d211 ',
' WPF ' = ' 81CDAB ',
' png ' = ' 89504e470d0a1a0a ',
' AW ' = ' 8a0109000000e108 ',
' Hap ' = ' 91334846 ',
' SKr ' = ' 9501 ',
' GPG ' = ' 99 ',
' PKR ' = ' 9901 ',
' Qdf ' = ' ac9ebd8f0000 ',
' PWL ' = ' E3828596 ',
' DCX ' = ' b168de3a ',
' Tib ' = ' b46e6844 ',
' ACS ' = ' C3abcdab ',
' LBK ' = ' C8007900 ',
' Class ' = ' Cafebabe ',
' dbx ' = ' cfad12fe ',
' Dot ' = ' d0cf11e0a1b11ae1 ',
' pps ' = ' d0cf11e0a1b11ae1 ',
' ppt ' = ' d0cf11e0a1b11ae1 ',
' Xla ' = ' d0cf11e0a1b11ae1 ',
' xls ' = ' d0cf11e0a1b11ae1 ',
' Wiz ' = ' d0cf11e0a1b11ae1 ',
' Ac_ ' = ' d0cf11e0a1b11ae1 ',
' ADP ' = ' d0cf11e0a1b11ae1 ',
' Apr ' = ' d0cf11e0a1b11ae1 ',
' MTW ' = ' d0cf11e0a1b11ae1 ',
' Opt ' = ' d0cf11e0a1b11ae1 ',
' Pub ' = ' d0cf11e0a1b11ae1 ',
' rvt ' = ' d0cf11e0a1b11ae1 ',
' Sou ' = ' d0cf11e0a1b11ae1 ',
' Spo ' = ' d0cf11e0a1b11ae1 ',
' VSD ' = ' d0cf11e0a1b11ae1 ',
' wps ' = ' d0cf11e0a1b11ae1 ',
' Ftr ' = ' d20a0000 ',
' ARL ' = ' d42a ',
' aut ' = ' d42a ',
' wmf ' = ' d7cdc69a ',
' EFX ' = ' Dcfe ',
' One ' = ' e4525c7b8cd8a74d ',
' rpm ' = ' edabeedb ',
' Gho ' = ' Feef ',
' GHS ' = ' feef ',
' WP ' = ' FF575043 ',
' WPD ' = ' FF575043 ',
' WPG ' = ' FF575043 ',
' WPP ' = ' FF575043 ',
' WP5 ' = ' FF575043 ',
' Wp6 ' = ' FF575043 ',
' Jfif ' = ' ffd8ff ',
' JPE ' = ' ffd8ff ',
' jpeg ' = ' ffd8ff ',
' jpg ' = ' ffd8ff ',
' MOF ' = ' fffe23006c006900 ',
' IPA ' = ' 504b03040a ',
);
/**
* Check File type
*
* @param string $filePath file path
* @param string $fileExt file extension
*
* @return Boolean
*/
public static function validation ($filePath, $FILEEXT)
{
File format Unknown
if (!isset (self::$_fileformats[$fileExt]))
{
return false;
}
$length = strlen (self::$_fileformats[$fileExt]);
$bin = Self::_readfile ($filePath, $length);
$fileHead = @unpack ("h{$length}", $bin);
Judging the file header
if (Strtolower (self::$_fileformats[$fileExt]) = = $fileHead [1])
{
return true;
}
return false;
}
/**
* Read File contents
*
* @param string $filePath file path
* @param integer $size
*
* @return String
*/
Private Function _readfile ($filePath, $size)
{
$file = fopen ($filePath, "RB");
$bin = Fread ($file, $size);
Fclose ($file);
return $bin;
}
}
?>
Call
Copy the Code code as follows:
Require './class_filetypevalidation.php ';
File path
$filePath = "D:/test.png";
$x = Filetypevalidation::validation ($filePath, ' zip ');
Var_dump ($x);
The above describes the win7 change file type PHP file header-based file type validation class functions, including the Win7 change the file type aspect of the content, I hope to be interested in PHP tutorial friends helpful.