自丢网

标题: dz!x上传图片时检测真实的文件类型,鉴别错误的扩展名 [打印本页]

作者: admin    时间: 2020-3-2 15:53
标题: dz!x上传图片时检测真实的文件类型,鉴别错误的扩展名
dz!x上传图片时检测真实的文件类型,鉴别错误的扩展名
应用场景: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,上传测试,失败则表示成功)。





欢迎光临 自丢网 (https://www.zidiu.com/) Powered by Discuz! X3.5