php判斷上傳檔案的檔案類型的幾種方法

來源:互聯網
上載者:User
  1. /**

  2. desc:判斷上傳檔案類型
  3. link:bbs.it-home.org
  4. date:2013/2/24
  5. */
  6. $array = array('jpg','gif','png','jpeg');
  7. $picImg ='/upfile/upload_pic/thumbnail_1258615556.jpg';

  8. $img = strtolower($picImg);

  9. //擷取檔案件副檔名方法一

  10. $ext = substr($img,strrpos($img,'.')+1);//這裡是讀取副檔名的代碼

  11. //擷取檔案件副檔名方法二

  12. $ext = end(explode('.',$img));

  13. //擷取檔案件副檔名方法三 這是應該是最安全的了,就是用php $_FILES['type']

  14. $ext = $_FILES['file']['type'];

  15. //擷取檔案件副檔名方法四

  16. $ext = getimagesize($img);//這個函數返回一個數組

  17. if( !in_array( $ext,$array ) )

  18. {
  19. exit('縮圖地址錯誤,請重新上傳!');
  20. }
  21. else
  22. {
  23. echo('你上傳的檔案類型不允許');
  24. exit;
  25. }

  26. /*

  27. strtolower 把字元大字轉換成小寫
  28. substr 字元截取,對中文處理不友好。
  29. strrpos 判斷字元出現在指定字串中的位置
  30. explode 分割函數,返回結果為數組
  31. end 讀取資料最後一個值
  32. $_FILES 全域變數檔案上傳
  33. getimagesize 擷取圖片的類型
  34. in_array 判斷當變數是否在數組中
  35. exit 終止當前指令碼運行
  36. */
  37. ?>

複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.