zoukankan      html  css  js  c++  java
  • 【CodeBase】PHP检查未知媒体文件的格式

    用法:

    <?php
    
    $filefullpath="F:/test/2awd45wr1e5fef5e5";
    echo Format::check($filefullpath,['png','jpg','gif']);

    格式检查类:

      1 <?php
      2 /**
      3  * lib_format.php
      4  * $Author: YunGaZeon $
      5  * $Blog: http://www.cnblogs.com/GaZeon/ $
      6  * $Id: lib_collection.php Date:2017-8-14 YunGaZeon $
      7 */
      8 
      9 class Format
     10 {
     11     public static function check($data,$formats=[])
     12     {
     13         $_fc=new FormatChecker;
     14 
     15         return $_fc->check($data,$formats);
     16     }
     17 }
     18 
     19 class FormatChecker
     20 {    
     21     private $_format=[
     22         'jp2' => '0000000C6A502020',
     23         '3gp' => '0000002066747970',
     24         '3gp5' => '0000001866747970',
     25         'm4a' => '00000020667479704D3441',
     26         'ico' => '00000100',
     27         'spl' => '00000100',
     28         'vob' => '000001BA',
     29         'cur' => '00000200',
     30         'wb2' => '00000200',
     31         'wk1' => '0000020006040600',
     32         'wk3' => '00001A0000100400',
     33         'wk4' => '00001A0002100400',
     34         'wk5' => '00001A0002100400',
     35         '123' => '00001A00051004',
     36         'qxd' => '00004D4D585052',
     37         'mdf' => '010F0000',
     38         'tr1' => '0110',
     39         'rgb' => '01DA01010003',
     40         'drw' => '07',
     41         'dss' => '02647373',
     42         'dat' => 'A90D000000000000',
     43         'db3' => '03',
     44         'qph' => '03000000',
     45         'adx' => '80000020031204',
     46         'db4' => '04',
     47         'n' => 'FFFE0000',
     48         'a' => 'FFFE0000',
     49         'skf' => '07534B46',
     50         'dtd' => '0764743264647464',
     51         'db' => 'D0CF11E0A1B11AE1',
     52         'pcx' => '0A050101',
     53         'mp' => '0CED',
     54         'doc' => 'D0CF11E0A1B11AE1',
     55         'nri' => '0E4E65726F49534F',
     56         'wks' => 'FF00020004040554',
     57         'pf' => '1100000053434341',
     58         'ntf' => '4E49544630',
     59         'nsf' => '4E45534D1A01',
     60         'arc' => '41724301',
     61         'pak' => '5041434B',
     62         'eth' => '1A350100',
     63         'mkv' => '1A45DFA393428288',
     64         'ws' => '1D7D',
     65         'gz' => '1F8B08',
     66         'tar.z' => '1FA0',
     67         'ain' => '2112',
     68         'lib' => '213C617263683E0A',
     69         'msi' => 'D0CF11E0A1B11AE1',
     70         'vmdk' => '4B444D',
     71         'dsp' => '23204D6963726F73',
     72         'amr' => '2321414D52',
     73         'hdr' => '49536328',
     74         'sav' => '24464C3240282329',
     75         'eps' => 'C5D0D3C6',
     76         'pdf' => '25504446',
     77         'fdf' => '25504446',
     78         'hqx' => '2854686973206669',
     79         'log' => '2A2A2A2020496E73',
     80         'ivr' => '2E524543',
     81         'rm' => '2E524D46',
     82         'rmvb' => '2E524D46',
     83         'ra' => '2E7261FD00',
     84         'au' => '646E732E',
     85         'cat' => '30',
     86         'evt' => '300000004C664C65',
     87         'asf' => '3026B2758E66CF11',
     88         'wma' => '3026B2758E66CF11',
     89         'wmv' => '3026B2758E66CF11',
     90         'wri' => 'BE000000AB',
     91         '7z' => '377ABCAF271C',
     92         'psd' => '38425053',
     93         'sle' => '414376',
     94         'asx' => '3C',
     95         'xdr' => '3C',
     96         'dci' => '3C21646F63747970',
     97         'manifest' => '3C3F786D6C2076657273696F6E3D',
     98         'xml' => '3C3F786D6C2076657273696F6E3D22312E30223F3E',
     99         'msc' => 'D0CF11E0A1B11AE1',
    100         'fm' => '3C4D616B65724669',
    101         'mif' => '56657273696F6E20',
    102         'gid' => '4C4E0200',
    103         'hlp' => '4C4E0200',
    104         'dwg' => '41433130',
    105         'syw' => '414D594F',
    106         'abi' => '414F4C494E444558',
    107         'aby' => '414F4C4442',
    108         'bag' => '414F4C2046656564',
    109         'idx' => '5000000020000000',
    110         'ind' => '414F4C494458',
    111         'pfc' => '414F4C564D313030',
    112         'org' => '414F4C564D313030',
    113         'vcf' => '424547494E3A5643',
    114         'bin' => '424C4932323351',
    115         'bmp' => '424D',
    116         'dib' => '424D',
    117         'prc' => '424F4F4B4D4F4249',
    118         'bz2' => '425A68',
    119         'tar.bz2' => '425A68',
    120         'tbz2' => '425A68',
    121         'tb2' => '425A68',
    122         'rtd' => '43232B44A4434DA5',
    123         'cbd' => '434246494C45',
    124         'iso' => '4344303031',
    125         'clb' => '434F4D2B',
    126         'cpt' => '43505446494C45',
    127         'cru' => '43525553482076',
    128         'swf' => '465753',
    129         'ctf' => '436174616C6F6720',
    130         'dms' => '444D5321',
    131         'adf' => '5245564E554D3A2C',
    132         'dvr' => '445644',
    133         'ifo' => '445644',
    134         'cdr' => '52494646',
    135         'vcd' => '454E545259564344',
    136         'e01' => '4C5646090D0AFF00',
    137         'evtx' => '456C6646696C6500',
    138         'qbb' => '458600000600',
    139         'cpe' => '464158434F564552',
    140         'flv' => '464C56',
    141         'aiff' => '464F524D00',
    142         'eml' => '582D',
    143         'gif' => '47494638',
    144         'pat' => '47504154',
    145         'gx2' => '475832',
    146         'sh3' => '4848474231',
    147         'tif' => '4D4D002B',
    148         'tiff' => '4D4D002B',
    149         'mp3' => '494433',
    150         'koz' => '49443303000000',
    151         'crw' => '49491A0000004845',
    152         'cab' => '4D534346',
    153         'lit' => '49544F4C49544C53',
    154         'chi' => '49545346',
    155         'chm' => '49545346',
    156         'jar' => '5F27A889',
    157         'jg' => '4A47040E000000',
    158         'kgb' => '4B47425F61726368',
    159         'shd' => '68490000',
    160         'lnk' => '4C00000001140200',
    161         'obj' => '80',
    162         'pdb' => 'ACED000573720012',
    163         'mar' => '4D41723000',
    164         'dmp' => '504147454455',
    165         'hdmp' => '4D444D5093A7',
    166         'mls' => '4D563243',
    167         'mmf' => '4D4D4D440000',
    168         'nvram' => '4D52564E',
    169         'ppz' => '4D534346',
    170         'snp' => '4D534346',
    171         'tlb' => '4D53465402000100',
    172         'dvf' => '4D535F564F494345',
    173         'msv' => '4D535F564F494345',
    174         'mid' => '4D546864',
    175         'midi' => '4D546864',
    176         'dsn' => '4D56',
    177         'com' => 'EB',
    178         'dll' => '4D5A',
    179         'drv' => '4D5A',
    180         'exe' => '4D5A',
    181         'pif' => '4D5A',
    182         'qts' => '4D5A',
    183         'qtx' => '4D5A',
    184         'sys' => 'FFFFFFFF',
    185         'acm' => '4D5A',
    186         'ax' => '4D5A900003000000',
    187         'cpl' => 'DCDC',
    188         'fon' => '4D5A',
    189         'ocx' => '4D5A',
    190         'olb' => '4D5A',
    191         'scr' => '4D5A',
    192         'vbx' => '4D5A',
    193         'vxd' => '4D5A',
    194         '386' => '4D5A',
    195         'api' => '4D5A900003000000',
    196         'flt' => '76323030332E3130',
    197         'zap' => '4D5A90000300000004000000FFFF',
    198         'sln' => '4D6963726F736F66742056697375616C',
    199         'jnt' => '4E422A00',
    200         'jtp' => '4E422A00',
    201         'cod' => '4E616D653A20',
    202         'dbf' => '4F504C4461746162',
    203         'oga' => '4F67675300020000',
    204         'ogg' => '4F67675300020000',
    205         'ogv' => '4F67675300020000',
    206         'ogx' => '4F67675300020000',
    207         'dw4' => '4F7B',
    208         'pgm' => '50350A',
    209         'pax' => '504158',
    210         'pgd' => '504750644D41494E',
    211         'img' => 'EB3C902A',
    212         'zip' => '504B0304140000',
    213         'docx' => '504B030414000600',
    214         'pptx' => '504B030414000600',
    215         'xlsx' => '504B030414000600',
    216         'kwd' => '504B0304',
    217         'odt' => '504B0304',
    218         'odp' => '504B0304',
    219         'ott' => '504B0304',
    220         'sxc' => '504B0304',
    221         'sxd' => '504B0304',
    222         'sxi' => '504B0304',
    223         'sxw' => '504B0304',
    224         'wmz' => '504B0304',
    225         'xpi' => '504B0304',
    226         'xps' => '504B0304',
    227         'xpt' => '5850434F4D0A5479',
    228         'grp' => '504D4343',
    229         'qemu' => '514649',
    230         'abd' => '5157205665722E20',
    231         'qsd' => '5157205665722E20',
    232         'reg' => 'FFFE',
    233         'sud' => '52454745444954',
    234         'ani' => '52494646',
    235         'cmx' => '52494646',
    236         'ds4' => '52494646',
    237         '4xm' => '52494646',
    238         'avi' => '52494646',
    239         'cda' => '52494646',
    240         'qcp' => '52494646',
    241         'rmi' => '52494646',
    242         'wav' => '52494646',
    243         'cap' => '58435000',
    244         'rar' => '526172211A0700',
    245         'ast' => '5343486C',
    246         'shw' => '53484F57',
    247         'cpi' => 'FF464F4E54',
    248         'sit' => '5374756666497420',
    249         'sdr' => '534D415254445257',
    250         'cnv' => '53514C4F434F4E56',
    251         'cal' => 'B5A2B0B3B3B0A5B5',
    252         'info' => 'E310000100000000',
    253         'uce' => '55434558',
    254         'ufa' => '554641C6D2C1',
    255         'pch' => '564350434830',
    256         'ctl' => '56455253494F4E20',
    257         'ws2' => '575332303030',
    258         'lwp' => '576F726450726F',
    259         'bdr' => '5854',
    260         'zoo' => '5A4F4F20',
    261         'ecf' => '5B47656E6572616C',
    262         'vcw' => '5B4D535643',
    263         'dun' => '5B50686F6E655D',
    264         'sam' => '5B7665725D',
    265         'cpx' => '5B57696E646F7773',
    266         'cfg' => '5B666C7473696D2E',
    267         'cas' => '5F434153455F',
    268         'cbk' => '5F434153455F',
    269         'arj' => '60EA',
    270         'vhd' => '636F6E6563746978',
    271         'csh' => '6375736800000002',
    272         'p10' => '64000000',
    273         'dex' => '6465780A30303900',
    274         'dsw' => '64737766696C65',
    275         'flac' => '664C614300000022',
    276         'dbb' => '6C33336C',
    277         'acd' => '72696666',
    278         'ram' => '727473703A2F2F',
    279         'dmg' => '78',
    280         'lgc' => '7B0D0A6F20',
    281         'lgd' => '7B0D0A6F20',
    282         'pwi' => '7B5C707769',
    283         'rtf' => '7B5C72746631',
    284         'psp' => '7E424B00',
    285         'wab' => '9CCBCB8D1375D211',
    286         'wpf' => '81CDAB',
    287         'png' => '89504E470D0A1A0A',
    288         'aw' => '8A0109000000E108',
    289         'hap' => '91334846',
    290         'skr' => '9501',
    291         'gpg' => '99',
    292         'pkr' => '9901',
    293         'qdf' => 'AC9EBD8F0000',
    294         'pwl' => 'E3828596',
    295         'dcx' => 'B168DE3A',
    296         'tib' => 'B46E6844',
    297         'acs' => 'C3ABCDAB',
    298         'lbk' => 'C8007900',
    299         'class' => 'CAFEBABE',
    300         'dbx' => 'CFAD12FE',
    301         'dot' => 'D0CF11E0A1B11AE1',
    302         'pps' => 'D0CF11E0A1B11AE1',
    303         'ppt' => 'D0CF11E0A1B11AE1',
    304         'xla' => 'D0CF11E0A1B11AE1',
    305         'xls' => 'D0CF11E0A1B11AE1',
    306         'wiz' => 'D0CF11E0A1B11AE1',
    307         'ac_' => 'D0CF11E0A1B11AE1',
    308         'adp' => 'D0CF11E0A1B11AE1',
    309         'apr' => 'D0CF11E0A1B11AE1',
    310         'mtw' => 'D0CF11E0A1B11AE1',
    311         'opt' => 'D0CF11E0A1B11AE1',
    312         'pub' => 'D0CF11E0A1B11AE1',
    313         'rvt' => 'D0CF11E0A1B11AE1',
    314         'sou' => 'D0CF11E0A1B11AE1',
    315         'spo' => 'D0CF11E0A1B11AE1',
    316         'vsd' => 'D0CF11E0A1B11AE1',
    317         'wps' => 'D0CF11E0A1B11AE1',
    318         'ftr' => 'D20A0000',
    319         'arl' => 'D42A',
    320         'aut' => 'D42A',
    321         'wmf' => 'D7CDC69A',
    322         'efx' => 'DCFE',
    323         'one' => 'E4525C7B8CD8A74D',
    324         'rpm' => 'EDABEEDB',
    325         'gho' => 'FEEF',
    326         'ghs' => 'FEEF',
    327         'wp' => 'FF575043',
    328         'wpd' => 'FF575043',
    329         'wpg' => 'FF575043',
    330         'wpp' => 'FF575043',
    331         'wp5' => 'FF575043',
    332         'wp6' => 'FF575043',
    333         'jfif' => 'FFD8FF',
    334         'jpe' => 'FFD8FF',
    335         'jpeg' => 'FFD8FF',
    336         'jpg' => 'FFD8FF',
    337         'mof' => 'FFFE23006C006900',
    338         'ipa' => '504B03040A'
    339     ];
    340     
    341     
    342     private $_allow_format=[];
    343     
    344     /*设置允许通过的格式*/
    345     public function allow($allow_format=[])
    346     {
    347         if(!empty($allow_format))
    348             $this->_allow_format=$allow_format;
    349     }
    350 
    351     /*根据长度读取文件二进制内容*/
    352     public function readHead($filepath,$size)
    353     {
    354         $fh=fopen($filepath,'rb');
    355         $bin=fread($fh,$size);
    356         fclose($fh);
    357         return $bin;
    358     }
    359     
    360     /*检查格式*/
    361     public function check($filepath,$allow_format=[])
    362     {
    363         $_allow_format=empty($allow_format)?$this->_allow_format:$allow_format;
    364         if(empty($filepath))
    365             return false;
    366         
    367         if(!empty($_allow_format))
    368         {
    369             foreach($_allow_format as $fm)
    370             {
    371                 if(!isset($this->_format[$fm]))
    372                     continue;
    373                 $len=strlen($this->_format[$fm]);
    374                 $binHead=$this->readHead($filepath,$len);
    375                 $fileHead=unpack('H'.$len,$binHead);
    376                 
    377                 if(strtoupper($fileHead[1])==$this->_format[$fm])
    378                     return $fm;
    379             }
    380         }
    381         else
    382         {
    383             foreach($this->_format as $fm=>$fbinhead)
    384             {
    385                 $len=strlen($fbinhead);
    386                 $binHead=$this->readHead($filepath,$len);
    387                 $fileHead=unpack('H'.$len,$binHead);
    388                 if(strtoupper($fileHead[1])==$fbinhead)
    389                     return $fm;
    390             }
    391         }
    392         return false;
    393     }
    394 }
  • 相关阅读:
    南京师范大学2021年高等代数考研试卷
    有限阶全图边图两种颜色后同色三角形数量最少为?(2019年清华大学丘成桐数学英才班)
    关于三个变元的正整数解(2019年清华大学丘成桐数学英才班)
    [Oracle工程师手记]归档日志产生量太大时的简易分析手段
    [Oracle 工程师手记] 如何查看 FRA 的使用率
    [Oracle数据库工程师手记] Data Guard broker 与 ORA-32701
    [Oracle工程师手记]CRSD 进程与 password 文件
    [oracle 工程师手记]RMAN duplicate 发生ORA-19504、ORA-17502、ORA-15001、ORA-27140 错误的解决过程
    [Oracle工程师手记] 备份恢复双城记(三)
    [Oracle工程师手记] 备份恢复双城记(二)
  • 原文地址:https://www.cnblogs.com/GaZeon/p/7362361.html
Copyright © 2011-2022 走看看