limit the format of uploaded files by judging Pdf,docx,xlsx,pptx,potx,vsdx,odt,doc,xls,ppt,vsd,pot,wps,dps,et and Txt,rtfFile type I hope you will make more comments!
- 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, the net search is C, signed integer, this will produce negative judgment is not normal
- $typeCode = Intval ($strInfo [' chars1 ']. $strInfo [' chars2 ']);
- EXEC ("File $filename", $output, $return _var);//Use Linux system command file to determine the type of upload file, mainly to determine the Txt,rtf file type
- $pattern = '/text,/';//rtf and TXT documents will be detected as text with 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 |