PHP判断图片是否为标准图片(防止篡改图片下传)

WBOY
Release: 2016-06-13 13:04:33
Original
780 people have browsed it

PHP判断图片是否为标准图片(防止篡改图片上传)

在项目安检时发现,某系项目中图片上传只是对后缀名进行了检查,导致含有某些代码的‘图片’也能上传到服务器,有重大隐患。写了一个方法,检验图片的正确性。(此方法无法完全验证,将图片源码中加了代码无法判断,不过将图片处理比如加水印以后,含有代码的图片在当作php执行时会失效)

?

/*
 *判断上传的图片是否为标准图片
 *$file $FILES['']获取的值
 *return 正常图片 true ;  异常图片 false;
 */
function isimage($file){
	if ($file["type"] == "image/gif") {
		@$im = imagecreatefromgif($file['tmp_name']);
	} elseif ($file["type"] == "image/png" || $file["type"] == "image/x-png") {
		@$im = imagecreatefrompng($file['tmp_name']);
	} else {
		@$im = imagecreatefromjpeg($file['tmp_name']);
	}
	if($im==false){
		return false;
	}else{
		return true;
	}

}
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!