PHP file type verification class function based on file header _ PHP Tutorial

Source: Internet
Author: User
PHP file type verification class function based on the file header. I wrote a verification class here to determine the file format through the file header. (It is not secure. if the user spoofs the file header, it can also pass verification.) copy the code as follows. I wrote a verification class here to determine the file format through the file header. (It is not secure. if the user spoofs the file header, it can pass verification)

The code is as follows:


/**
* Document type
*
* @ Author Silver
*/
Class FileTypeValidation
{
// File type, different header information
Private static $ _ fileFormats = Array (
'Jp2' => '0000000c6a502020 ',
'3gp '=> '123 ',
& Apos; 3gp5 & apos; = & apos; 0000001866747970 & apos ',
'M4a '=> '00000020667479704d3441 ',
'Ico '=> '123 ',
'Spl' => '123 ',
'Vob' => '0001ba ',
& Apos; cur & apos; = & apos; 00000200 & apos ',
'Wb2' => '123 ',
'Wk1 '=> '123 ',
'Wk3' => '20171a0000100400 ',
'Wk4 '=> '20171a0002100400 ',
'Wk5 '=> '20171a0002100400 ',
'20140901' => '20171a00051004 ',
'Qxd '=> '20174d4d585052 ',
'Mdf '=> '010f0000 ',
'Tr1' => '123 ',
'Rgb '=> '01da01010003 ',
'Drw' => '07 ',
'Dss '=> '123 ',
'Dat '=> 'a90d000000000000 ',
'Db3' => '03 ',
'Qph' => '123 ',
'Adx' => '123 ',
'Db4' => '04 ',
'N' => 'fffe000000 ',
'A' => 'fffe000000 ',
'Skf '=> '07534b46 ',
'Dtd '=> '123 ',
'DB' => 'd0cf11e0a1b11ae1 ',
'Pcx' => '0a050101 ',
'MP' => '0ced ',
'Doc' => 'd0cf11e0a1b11ae1 ',
'Nri' => '0e4e65108f49534f ',
'Wk' => 'ff00020004040554 ',
'Pf '=> '123 ',
'Ntf' => '4e49544630 ',
'NSF '=> '4e45534d1a01 ',
'Arc' => '123 ',
'Pak '=> '5041434b ',
'Eth '=> '1a350100 ',
'Mkv '=> '1a45dfa393428288 ',
'Ws '=> '1d7d ',
'Gz '=> '1f8b08 ',
'Tar. Z' => '1fa0 ',
'Ain '=> '123 ',
'Lib' => 'Export c617264243e0a ',
'Msi '=> 'd0cf11e0a1b11ae1 ',
'Vmdk' => '4b444d ',
'Dsps '=> '23204d6963726f73 ',
'Amr' => '2321414d52 ',
& Apos; hdr & apos; = & apos; 49536328 & apos ',
'Sav' => '24464c3240282329 ',
'Eps '=> 'c5d0d3c6 ',
'PDF '=> '123 ',
'Fdf '=> '123 ',
'Hqx' => '123 ',
'Log' => '2a2a2a2020496e73 ',
'Ivr' => '2e524543 ',
'Rm '=> '2e524d46 ',
'Rmvb '=> '2e524d46 ',
'A' => '2e00001fd00 ',
'Au '=> '646e732e ',
'Cat' => '30 ',
'Evt' => '300000004c664c65 ',
'Asf' => '3026b2758e66cf11 ',
'Wma' => '3026b2758e66cf11 ',
'Wmv '=> '3026b2758e66cf11 ',
'Wri' => 'bedomain00ab ',
'7z' => '377abcaf271c ',
'Psd '=> '123 ',
'Sync' => '123 ',
'Asx' => '3C ',
'Xdr '=> '3C ',
'Dci' => '3c21646f63747970 ',
'Manifest '=> '3c3f786d6c2076657272136f6e3d ',
'Xml' => '3c3f786d6c2076657272136f6e3d22312e30223f3e ',
'Msc '=> 'd0cf11e0a1b11ae1 ',
'FM '=> '3c4d616b65724669 ',
'If' => '56657272136f6e20 ',
'Gid' => '4c4e0200 ',
'Hlp '=> '4c4e0200 ',
'Dwg '=> '123 ',
'Syw' => '414d594f ',
'Abi '=> '414f4c494e444558 ',
'Aby' => '414f4c4442 ',
'Bag' => '414f4c2046656564 ',
'Idx' => '123 ',
'IND '=> '414f4c494458 ',
'Pfc '=> '414f4c564d313030 ',
'Org '=> '414f4c564d313030 ',
'Vcf '=> '2017547494e3a5643 ',
'Bin' => 'Export c4932323351 ',
'Bmp '=> 'mongod ',
'Dib' => 'did ',
'Prc' => '0000f4f4b4d4f00009 ',
'Bz2' => '2017a68 ',
'Tar.bz2 '=> '2017a68 ',
'Tbz2' => '0000a68 ',
'Tb2' => '2017a68 ',
'Rtd' => '43232b44a4434da5 ',
'CBD '=> '434246494c45 ',
'ISO '=> '123 ',
'Clb' => '434f4d2b ',
'Cpt' => '43505446494c45 ',
'Cru' => '000000 ',
'Swf '=> '123 ',
'Ctf' => '436174616c6f6720 ',
'DMs' => '444d5321 ',
'Adf' => '5245564e554d3a2c ',
'DVR' => '123 ',
'Ifo' => '123 ',
'Cdrs '=> '123 ',
'Vcd' => '454e545259564344 ',
'Mdi '=> '123 ',
'E01' => '4c5646090d0aff00 ',
'Evtx '=> '456c6646696c6500 ',
'Qbb' => '123 ',
'Cpe '=> '464158434f564552 ',
'Flv' => '464c56 ',
'Aiff '=> '464f524d00 ',
'Yml' => '582d ',
'Gif' => '123 ',
'Pat '=> '123 ',
'Gx2 '=> '123 ',
'Sh3' => '123 ',
'Tif '=> '4d4d002b ',
'Tiff '=> '4d4d002b ',
'Mp3' => '123 ',
'Koz' => '123 ',
'Crw' => '49491a0000004845 ',
'Cab '=> '4d534346 ',
'Command' => '49544f4c49544c53 ',
'Chi '=> '123 ',
'Chm' => '123 ',
'Jar' => '5f27a889 ',
'JG' => '4a47040e000000 ',
'K' => '4b47213f61726368 ',
'Shd' => '123 ',
'Lnk '=> '4c00000001140200 ',
'Obj '=> '80 ',
'Pdb' => 'aced000573720012 ',
'Mar '=> '4d000023000 ',
'Dmp '=> '123 ',
'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 ',
'000000' => '4d5a ',
'Api' => '4d5a900003000000 ',
'Flt' => '76323030332e3130 ',
'Zap '=> '4d5a90000300000004000000ffff ',
'Sln' => '4d6963726f736f66742056697375616c ',
'Jnt '=> '4e422a00 ',
'Jtp '=> '4e422a00 ',
'Cod '=> '4e616d653a20 ',
'Dbf' => '4f504c4461746162 ',
'Oga' => '4f67675300020000 ',
'Ogy' => '4f67675300020000 ',
'Ogv' => '4f67675300020000 ',
'Ogx' => '4f67675300020000 ',
'Dw4' => '4f7b ',
'Pgm '=> '50350a ',
'Pax' => '123 ',
'Pgd '=> '504750644d41494e ',
'IMG '=> 'eb3c902a ',
'Zip' => '504b0304140000 ',
'Docx' => '504b030414000600 ',
'Ppt' => '504b030414000600 ',
'Xlsx' => '504b030414000600 ',
'Kwd '=> '504b0304 ',
'Odt' => '504b0304 ',
'Odp' => '504b0304 ',
'Ott '=> '504b0304 ',
'Sxc '=> '504b0304 ',
'Sxd '=> '504b0304 ',
'Ssi' => '504b0304 ',
'Sxw' => '504b0304 ',
'Wmz' => '504b0304 ',
'Xpi '=> '504b0304 ',
'Xps '=> '504b0304 ',
'Xpt' => '5850434f4d0a5479 ',
'Grp '=> '504d4343 ',
'Qemu' => '123 ',
'Abd' => '5157205665722e20 ',
'Qsd' => '5157205665722e20 ',
'Reg '=> 'fffe ',
'Sud' => '123 ',
'Ani '=> '123 ',
'Cmx' => '20140901 ',
'Ds4' => '123 ',
'4xm '=> '123 ',
'Av' => '123 ',
'Cda' => '123 ',
'Qcp' => '123 ',
'Rummi' => '123 ',
'Wav '=> '123 ',
'Cap' => '123 ',
'Rar '=> '526172211a0700 ',
'Ast '=> '5343486c ',
'Shw' => '53484f57 ',
'CPI '=> 'ff464f4e54 ',
'Sit '=> '123 ',
'Sdr '=> '534d2017254445257 ',
'Fcn' => '53514c4f434f4e56 ',
'Cal '=> 'b5a2b0b3b0a5b5 ',
'Info' => 'e31000000000000 ',
'Uce '=> '123 ',
'Ufa '=> '554641c6d2c1 ',
'Pch' => '123 ',
'Ctl '=> '56455253494f4e20 ',
'Ws2 '=> '123 ',
'Lwp '=> '576f000000000000f ',
'Bdr '=> '123 ',
'Zoo' => '5a4f4f20 ',
'Ecf' => '5b47656e6538516c ',
'Vcw' => '5b4d535643 ',
'Dun' => '5b50686f6e655d ',
'Sam '=> '5b7665725d ',
'Cpx' => '5b57696e646f7773 ',
'Cfg '=> '5b666c7471_6d2e ',
'Cas '=> '5f434153455f ',
'Cbk' => '5f434153455f ',
'Arj' => '60ea ',
'Vhd '=> '636f6e6563746978 ',
'CSH' => '123 ',
'P10' => '123 ',
'Dex '=> '6465780a30303900 ',
'Dsw' => '64737766696c65 ',
'Flac '=> '664c614300000022 ',
'Dbb' => '6c33336c ',
'ACD' => '123 ',
'Ram' => '727473703a2f2f ',
'Dmg '=> '78 ',
'Lgc '=> '7b0d0a8f20 ',
'Lgd '=> '7b0d0a6f20 ',
'Pwi '=> '7b5c707769 ',
'Rtf '=> '7b5c72746631 ',
'PSP '=> '7e0000b00 ',
'Wab' => '9ccbcb8d1375d211 ',
'Wpf '=> '81cdab ',
'PNG '=> '89504e470d0a1a0a ',
'Aw' => '8a01090020.e108 ',
'Ha' => '123 ',
'Skr' => '123 ',
'Gpg' => '99 ',
'Pkr' => '123 ',
'Qdf '=> 'ac9ebd8f0000 ',
'Pwl' => 'e3828596 ',
'Dcx' => 'b168de3a ',
'Tid' => '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 ',
'Rdv' => 'd0cf11e0a1b11ae1 ',
'Sou' => 'd0cf11e0a1b11ae1 ',
'SPO' => 'd0cf11e0a1b11ae1 ',
'Password' => 'd0cf11e0a1b11ae1 ',
'Wps' => 'd0cf11e0a1b11ae1 ',
'Ftr' => 'd20a0000 ',
'Arl' => 'd42a ',
'Aut' => 'd42a ',
'WMF '=> 'd7csc69a ',
'Efx' => 'dcfe ',
'One' => 'e4525c7b8cd8a74d ',
'RP' => '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 the file type
*
* @ Param string $ filePath file path
* @ Param string $ fileExt file extension
*
* @ Return boolean
*/
Public static function validation ($ filePath, $ fileExt)
{
// Unknown file format
If (! Isset (self ::$ _ fileFormats [$ fileExt])
{
Return false;
}
$ Length = strlen (self: $ _ fileFormats [$ fileExt]);
$ Bin = self: _ readFile ($ filePath, $ length );
$ FileHead = @ unpack ("H {$ length}", $ bin );
// Determine the file header
If (strtolower (self: $ _ fileFormats [$ fileExt]) = $ fileHead [1])
{
Return true;
}
Return false;
}
/**
* Reading file content
*
* @ 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

The code is as follows:


Require './class_filetypevalidation.php ';
// File path
$ FilePath = "D:/test.png ";
$ X = FileTypeValidation: validation ($ filePath, 'Zip ');
Var_dump ($ x );

Authentication. (it is not secure. if the user spoofs the file header, it can also pass verification.) the code is as follows...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.