The format of uploaded files is limited to
PDF,docx,xlsx,pptx,potx,vsdx,odt,doc,xls,ppt,vsd,pot,wps,dps,et and txt,rtfFile types
I hope everyone can give us more opinions!
- function checkFileType($filename){
- //File header
- $_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); //Read only 2 bytes
- fclose($file);
- $strInfo = @unpack("C2chars", $bin);//C is an unsigned integer. All those found on the Internet are c, which is a signed integer. This will cause abnormal negative number judgment
- $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
- exec("file $filename",$output,$return_var);//Use the linux system command file to determine the type of uploaded file , mainly to determine the txt and rtf file types
- $pattern = '/text,/'; //rtf and txt documents will be detected as text using file detection
- $_count = preg_match($pattern,strrchr($output[0] ,":"));
- echo $typeCode;
- if(in_array($typeCode,$_typecode) || $_count == 1) {
- return true;
- }else{
- return false;
- }
- }
Copy code
|