建筑资源吧 發表於 2019-7-13 19:57:08

上传图片时检测真实的文件类型,鉴别错误的扩展名

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