用法:

<?php

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

格式检查类:

 <?php
/**
* lib_format.php
* $Author: YunGaZeon $
* $Blog: http://www.cnblogs.com/GaZeon/ $
* $Id: lib_collection.php Date:2017-8-14 YunGaZeon $
*/ class Format
{
public static function check($data,$formats=[])
{
$_fc=new FormatChecker; return $_fc->check($data,$formats);
}
} class FormatChecker
{
private $_format=[
'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',
'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'
]; private $_allow_format=[]; /*设置允许通过的格式*/
public function allow($allow_format=[])
{
if(!empty($allow_format))
$this->_allow_format=$allow_format;
} /*根据长度读取文件二进制内容*/
public function readHead($filepath,$size)
{
$fh=fopen($filepath,'rb');
$bin=fread($fh,$size);
fclose($fh);
return $bin;
} /*检查格式*/
public function check($filepath,$allow_format=[])
{
$_allow_format=empty($allow_format)?$this->_allow_format:$allow_format;
if(empty($filepath))
return false; if(!empty($_allow_format))
{
foreach($_allow_format as $fm)
{
if(!isset($this->_format[$fm]))
continue;
$len=strlen($this->_format[$fm]);
$binHead=$this->readHead($filepath,$len);
$fileHead=unpack('H'.$len,$binHead); if(strtoupper($fileHead[1])==$this->_format[$fm])
return $fm;
}
}
else
{
foreach($this->_format as $fm=>$fbinhead)
{
$len=strlen($fbinhead);
$binHead=$this->readHead($filepath,$len);
$fileHead=unpack('H'.$len,$binHead);
if(strtoupper($fileHead[1])==$fbinhead)
return $fm;
}
}
return false;
}
}

【CodeBase】PHP检查未知媒体文件的格式的更多相关文章

  1. Mac OS环境下媒体文件分割工具mediafilesegmenter的简单使用(生成M3U8 TS文件)

    mediafilesegmenter是苹果开发的一款用于分割媒体文件的工具,其功能与mediastreamsegmenter相似,但操作更简单. * 具体可以对比博客中的另一篇简介<Mac OS ...

  2. 幻世(OurDream)2D图形引擎使用教程11——播放媒体文件(1)

    声明:本教程版权归Lizcst Software Lab所有,欢迎转载,但是转载必须保留本段声明文字,并注明文章来源:http://blog.csdn.net/kflizcst 谢谢合作! 播放媒体是 ...

  3. 开源的.NET媒体文件操作组件TagLib#解析

    人生得意须尽欢 莫使金樽空对月.写博客都会在吃饭后,每次吃饭都要喝上二两小酒,写博客前都要闲扯,这些都是个人爱好,改不掉了,看不惯的人,还望多多包含一下,有相同爱好的同学,咱们可以一起喝着小酒一边吹牛 ...

  4. FFmpeg命令行工具学习(三):媒体文件转换工具ffmpeg

    一.简述 ffmpeg是一个非常强大的工具,它可以转换任何格式的媒体文件,并且还可以用自己的AudioFilter以及VideoFilter进行处理和编辑.有了它,我们就可以对媒体文件做很多我们想做的 ...

  5. 使用mediainfo工具统计每个视频文件(媒体文件)播放时长

    需求 1.运营那边需要统计大量视频文件的播放时长,并汇总记录到excel表中,问我有什么方法搞定 这边搜索了很多统计媒体文件时长的,主要有以下几种 1.使用java获取 2.使用python获取 3. ...

  6. FFmpeg命令行工具学习(二):播放媒体文件的工具ffplay

    一.简述 ffplay是以FFmpeg框架为基础,外加渲染音视频的库libSDL构建的媒体文件播放器. 在使用ffplay之前必须要安装到系统中,MAC的安装教程为:http://www.cnblog ...

  7. convert 批量文件的格式转换

    1.将 a.gif 转为 png 格式 convert a.gif a.png 请注意,convert 命令的基本格式为 convert 源文件 [参数] 目标文件 在上面的命令中,源文件是 a.gi ...

  8. 媒体文件audio 转 base64 编码 (利用 FileReader & Audio 对象)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. WPF中播放声音媒体文件

    这段时间我们小组要给部门的Annual Meeting準备一个WPF的抽奖程序,为了增加程序的有趣性,我们在程序中需要播放背景音乐等.由於对之前从未使用过WPF,所以对其中的声音等媒体文件播放不是很清 ...

随机推荐

  1. mysql主从复制之同步部分库表

    这里以mariadb为例,和mysql一样的配置 系统:centos7 主服务器:192.168.0.1:3305(两台服务器都做过时间同步) 从服务器:192.168.0.2:3306(两台服务器都 ...

  2. 记录一个在线压缩和还原压缩js代码的工具

    packer – javascript 压缩工具 http://dean.edwards.name/packer/ Javascript Beautifier ---可以恢复某些压缩工具压缩的js代码 ...

  3. QQ的全国地址编码

    D:\Program Files (x86)\Tencent\QQ\I18N\2052\LocList.xml

  4. 2 - Bootstrap-引导类-Bootstrap/ServerBootstrap

    2.1 ChannelOption和属性 //设置属性 bootstrap.option("xxx", 1231231); bootstrap.attr("xxx&quo ...

  5. launchctl

    Launchctl 系统启动时, 系统会以root用户的身份扫描/System/Library/LaunchDaemons和/Library/LaunchDaemons目录, 如果文件中有Disabl ...

  6. 16.Ubuntu LTS 16.04安装搜狗输入法全过程记录(纯新手)

    这是我第四次打算转到Ubuntu上了,应该不会像以前那样装个系统就拜拜了.打算先把C和Vim重新学起来,数据结构那本书看完写完,第二步是学python和算法导论,暂定如此. 昨天晚上系统装完以后想着要 ...

  7. [Java][Liferay] 解决Liferay ext项目deploy的问题

    Liferay ext project在install war包之后需要重启服务器,重启服务器中会执行ExtHotDeployListener中的逻辑,这里有一个坑,如果是第二次以后install e ...

  8. JAVA4大线程池

    不知不觉中我们电脑的硬件设施越来越好,从双核四线程普及到如今四核八线比比皆是.互联网发展至今,讲究的就是快,less is more,而且大数据的诞生和各种种类繁多的需求处理,单线程的程序逐渐不能满足 ...

  9. Git 常用基本命令汇总

    安装git yum install -y curl-devel expat-devel gettext-devel openssl-devel zlib-devel yum install -y gi ...

  10. Every ending is just a new beginning.

    Every ending is just a new beginning.每次结束都是新的开始.