Home > Backend Development > PHP Tutorial > php+shell detects file type

php+shell detects file type

WBOY
Release: 2016-07-25 09:01:36
Original
1137 people have browsed it
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!
  1. function checkFileType($filename){
  2. //File header
  3. $_typecode = array(
  4. '3780',//PDF
  5. '8075',//.docx,.xlsx,.pptx,.potx,. vsdx,.odt
  6. '208207',//.doc,.xls,.ppt,.vsd,.pot,.wps,.dps,.et
  7. );
  8. $file = fopen($filename, "rb") ;
  9. //contents = stream_get_contents($file);
  10. //$contents = fread($file, filesize($filename));
  11. $bin = fread($file, 2); //Read only 2 bytes
  12. fclose($file);
  13. $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
  14. $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
  15. 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
  16. $pattern = '/text,/'; //rtf and txt documents will be detected as text using file detection
  17. $_count = preg_match($pattern,strrchr($output[0] ,":"));
  18. echo $typeCode;
  19. if(in_array($typeCode,$_typecode) || $_count == 1) {
  20. return true;
  21. }else{
  22. return false;
  23. }
  24. }
Copy code


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template