如何通过PHP脚本检测文件的真实类型?
- 内容介绍
- 文章标签
- 相关推荐
本文共计312个文字,预计阅读时间需要2分钟。
通过判断,限制上传文件的格式为PDF、docx、xlsx、pptx、potx、vsdx、odt、doc、xls、ppt、vsd、pot、wps、dps、e+t和txt、rtf文件类型。希望大家多提意见!
PHPfunction checkFileType($filename) { // 文件头 $typecode=array( '378'=> 'PDF', '504'=> 'docx', '507'=> 'xlsx', '524'=> 'pptx', '524'=> 'potx', '524'=> 'vsdx', '504'=> 'odt', '424'=> 'doc', '507'=> 'xls', '524'=> 'ppt', '524'=> 'vsd', '524'=> 'pot', '524'=> 'wps', '524'=> 'dps', '524'=> 'e+t', '436'=> 'txt', '7173'=> 'rtf' );
// 获取文件头 $fileheader=fread(fopen($filename, rb), 512); $fileheader=bin2hex($fileheader);
// 判断文件类型 foreach ($typecode as $code=> $type) { if (strpos($fileheader, $code) !==false) { return $type; } }
return 'Invalid file type';}
通过判断 限制上传文件的格式为PDF,docx,xlsx,pptx,potx,vsdx,odt,doc,xls,ppt,vsd,pot,wps,dps,e t和txt,rtf文件类型希望大家多提意见!
[PHP]代码
function checkFileType($filename){ //文件头 $_typecode = array( '3780',//PDF '8075',//.docx,.xlsx,.pptx,.potx,.vsdx,.odt '208207',//.doc,.xls,.ppt,.vsd,.pot,.wps,.dps,.et ); $file = fopen($filename, "rb"); //contents = stream_get_contents($file); //$contents = fread($file, filesize($filename)); $bin = fread($file, 2); //只读2字节 fclose($file); $strInfo = @unpack("C2chars", $bin);// C为无符号整数,网上搜到的都是c,为有符号整数,这样会产生负数判断不正常 $typeCode = intval($strInfo['chars1'].$strInfo['chars2']); exec("file $filename",$output,$return_var);//用linux系统命令file判断上传文件的类型,主要是判断txt,rtf文件类型 $pattern = '/text,/';//rtf和txt文档用file检测都会被检测为text $_count = preg_match($pattern,strrchr($output[0],":")); echo $typeCode; if(in_array($typeCode,$_typecode) || $_count == 1) { return true; }else{ return false; } }
本文共计312个文字,预计阅读时间需要2分钟。
通过判断,限制上传文件的格式为PDF、docx、xlsx、pptx、potx、vsdx、odt、doc、xls、ppt、vsd、pot、wps、dps、e+t和txt、rtf文件类型。希望大家多提意见!
PHPfunction checkFileType($filename) { // 文件头 $typecode=array( '378'=> 'PDF', '504'=> 'docx', '507'=> 'xlsx', '524'=> 'pptx', '524'=> 'potx', '524'=> 'vsdx', '504'=> 'odt', '424'=> 'doc', '507'=> 'xls', '524'=> 'ppt', '524'=> 'vsd', '524'=> 'pot', '524'=> 'wps', '524'=> 'dps', '524'=> 'e+t', '436'=> 'txt', '7173'=> 'rtf' );
// 获取文件头 $fileheader=fread(fopen($filename, rb), 512); $fileheader=bin2hex($fileheader);
// 判断文件类型 foreach ($typecode as $code=> $type) { if (strpos($fileheader, $code) !==false) { return $type; } }
return 'Invalid file type';}
通过判断 限制上传文件的格式为PDF,docx,xlsx,pptx,potx,vsdx,odt,doc,xls,ppt,vsd,pot,wps,dps,e t和txt,rtf文件类型希望大家多提意见!
[PHP]代码
function checkFileType($filename){ //文件头 $_typecode = array( '3780',//PDF '8075',//.docx,.xlsx,.pptx,.potx,.vsdx,.odt '208207',//.doc,.xls,.ppt,.vsd,.pot,.wps,.dps,.et ); $file = fopen($filename, "rb"); //contents = stream_get_contents($file); //$contents = fread($file, filesize($filename)); $bin = fread($file, 2); //只读2字节 fclose($file); $strInfo = @unpack("C2chars", $bin);// C为无符号整数,网上搜到的都是c,为有符号整数,这样会产生负数判断不正常 $typeCode = intval($strInfo['chars1'].$strInfo['chars2']); exec("file $filename",$output,$return_var);//用linux系统命令file判断上传文件的类型,主要是判断txt,rtf文件类型 $pattern = '/text,/';//rtf和txt文档用file检测都会被检测为text $_count = preg_match($pattern,strrchr($output[0],":")); echo $typeCode; if(in_array($typeCode,$_typecode) || $_count == 1) { return true; }else{ return false; } }

