久久久久av_欧美日韩一区二区在线_国产精品三区四区_日韩中字在线

Discuz! 官方交流社區

標題: 上傳圖片時檢測真實的文件類型,鑒別錯誤的擴展名 [打印本頁]

作者: 建筑資源吧    時間: 2019-7-13 19:57
標題: 上傳圖片時檢測真實的文件類型,鑒別錯誤的擴展名
本帖最后由 建筑資源吧 于 2019-7-13 19:59 編輯

應用場景:JPG格式具有高壓縮率,更適于互聯網傳播,本站僅支持上傳JPG格式的圖片,但部分用戶會直接將 PNG 或 BMP 格式的圖片修改擴展名為 JPG,繞開系統限制。可采用以下方法進行檢測:

1、打開 source/class/discuz/discuz_upload.php 文件,查找:
  1. $attach['size'] = intval($attach['size']);
復制代碼
在其前方增加:
  1. $filename = $attach['tmp_name'];
  2. $file = fopen($filename, "rb");
  3. $bin = fread($file, 2);
  4. fclose($file);
  5. $strinfo = @unpack("C2chars", $bin);
  6. $typecode = intval($strinfo['chars1'].$strinfo['chars2']);
  7. if(in_array($typecode, array('7173', '6677', '13780'))) {
  8. $attach['unsupported_image_format'] = 1;
  9. }
復制代碼

2、打開 source/class/forum/forum_upload.php 文件,查找:
  1. loadcache('attachtype');
復制代碼

在其前方增加:
  1. if($upload->attach['unsupported_image_format'] == '1') {
  2.                         return $this->uploadmsg(51);
  3.                 }
復制代碼

3、打開 static/js/forum_post.js 文件,查找:        
  1. '11' : '今日您已無法上傳那么大的附件'
復制代碼

將其替換為:
  1. '11' : '今日您已無法上傳那么大的附件',
  2. '51' : '圖像格式不支持,請轉換為 .JPG'
復制代碼

更新緩存,刷新發帖頁面后測試(將一張PNG格式圖片的擴展名修改為JPG,上傳測試,失敗則表示成功)。






歡迎光臨 Discuz! 官方交流社區 (http://www.9999xn.com/) Powered by Discuz! W1.0