-
- //Detect file type
- //by http://bbs.it-home.org
- 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 judgment of negative numbers
- $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, rtf file type
- $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
|