Php+shell Detecting file types

Source: Internet
Author: User
Tags fread
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!
  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, the net search is C, signed integer, this will produce negative judgment is not normal
  14. $typeCode = Intval ($strInfo [' chars1 ']. $strInfo [' chars2 ']);
  15. 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
  16. $pattern = '/text,/';//rtf and TXT documents will be detected as text with 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 Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.