php檢測上傳excel檔案類型的範例程式碼

來源:互聯網
上載者:User
  1. /**
  2. * Detect upload file type
  3. * 檢測上傳檔案的excel檔案類型
  4. * @param array $file
  5. * @return bool $flag
  6. * @site bbs.it-home.org
  7. */
  8. private function detectUploadFileMIME($file) {
  9. // 1.through the file extension judgement 03 or 07
  10. $flag = 0;
  11. $file_array = explode ( ".", $file ["name"] );
  12. $file_extension = strtolower ( array_pop ( $file_array ) );
  13. // 2.through the binary content to detect the file
  14. switch ($file_extension) {
  15. case "xls" :
  16. // 2003 excel
  17. $fh = fopen ( $file ["tmp_name"], "rb" );
  18. $bin = fread ( $fh, 8 );
  19. fclose ( $fh );
  20. $strinfo = @unpack ( "C8chars", $bin );
  21. $typecode = "";
  22. foreach ( $strinfo as $num ) {
  23. $typecode .= dechex ( $num );
  24. }
  25. if ($typecode == "d0cf11e0a1b11ae1") {
  26. $flag = 1;
  27. }
  28. break;
  29. case "xlsx" :
  30. // 2007 excel
  31. $fh = fopen ( $file ["tmp_name"], "rb" );
  32. $bin = fread ( $fh, 4 );
  33. fclose ( $fh );
  34. $strinfo = @unpack ( "C4chars", $bin );
  35. $typecode = "";
  36. foreach ( $strinfo as $num ) {
  37. $typecode .= dechex ( $num );
  38. }
  39. echo $typecode;
  40. if ($typecode == "504b34") {
  41. $flag = 1;
  42. }
  43. break;
  44. }
  45. // 3.return the flag
  46. return $flag;
  47. }
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.