From a3de59f7d00e01366e2920dc3a206f8c9e217db7 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 22 Jul 2016 12:21:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BFile=E7=B1=BB=E5=92=8CValidat?= =?UTF-8?q?e=E7=B1=BB=E7=9A=84=E5=9B=BE=E5=83=8F=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/File.php | 13 ++++++++++++- library/think/Validate.php | 13 ++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/library/think/File.php b/library/think/File.php index 21e8c9dd..bd39209f 100644 --- a/library/think/File.php +++ b/library/think/File.php @@ -195,12 +195,23 @@ class File extends SplFileObject { $extension = strtolower(pathinfo($this->getInfo('name'), PATHINFO_EXTENSION)); /* 对图像文件进行严格检测 */ - if (in_array($extension, ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf']) && !in_array(exif_imagetype($this->filename), [1, 2, 3, 4, 6])) { + if (in_array($extension, ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf']) && !in_array($this->getImageType($this->filename), [1, 2, 3, 4, 6])) { return false; } return true; } + // 判断图像类型 + protected function getImageType($image) + { + if (function_exists('exif_imagetype')) { + return exif_imagetype($image); + } else { + $info = getimagesize($image); + return $info[2]; + } + } + /** * 检测上传文件大小 * @param integer $size 最大大小 diff --git a/library/think/Validate.php b/library/think/Validate.php index a88043a2..91707274 100644 --- a/library/think/Validate.php +++ b/library/think/Validate.php @@ -561,7 +561,7 @@ class Validate $result = $value instanceof \think\File; break; case 'image': - $result = $value instanceof \think\File && in_array(exif_imagetype($value->getRealPath()), [1, 2, 3, 6]); + $result = $value instanceof \think\File && in_array($this->getImageType($value->getRealPath()), [1, 2, 3, 6]); break; default: if (isset(self::$type[$rule])) { @@ -575,6 +575,17 @@ class Validate return $result; } + // 判断图像类型 + protected function getImageType($image) + { + if (function_exists('exif_imagetype')) { + return exif_imagetype($image); + } else { + $info = getimagesize($image); + return $info[2]; + } + } + /** * 验证是否为合格的域名或者IP 支持A,MX,NS,SOA,PTR,CNAME,AAAA,A6, SRV,NAPTR,TXT 或者 ANY类型 * @access protected