dz!x上传图片时检测真实的文件类型,鉴别错误的扩展名
应用场景:JPG格式具有高压缩率,更适于互联网传播,本站仅支持上传JPG格式的图片,但部分用户会直接将 PNG 或 BMP 格式的图片修改扩展名为 JPG,绕开系统限制。可采用以下方法进行检测:
1、打开 source/class/discuz/discuz_upload.php 文件,查找:
- $attach['size'] = intval($attach['size']);
复制代码 在其前方增加:
- $filename = $attach['tmp_name'];
- $file = fopen($filename, "rb");
- $bin = fread($file, 2);
- fclose($file);
- $strinfo = @unpack("C2chars", $bin);
- $typecode = intval($strinfo['chars1'].$strinfo['chars2']);
- if(in_array($typecode, array('7173', '6677', '13780'))) {
- $attach['unsupported_image_format'] = 1;
- }
复制代码 2、打开 source/class/forum/forum_upload.php 文件,查找:
在其前方增加:
- if($upload->attach['unsupported_image_format'] == '1') {
- return $this->uploadmsg(51);
- }
复制代码 3、打开 static/js/forum_post.js 文件,查找:
将其替换为:
- '11' : '今日您已无法上传那么大的附件',
- '51' : '图像格式不支持,请转换为 .JPG'
复制代码 更新缓存,刷新发帖页面后测试(将一张PNG格式图片的扩展名修改为JPG,上传测试,失败则表示成功)。
|
|