diff --git a/application/api/common.php b/application/api/common.php index 6aef27c..f2d073c 100644 --- a/application/api/common.php +++ b/application/api/common.php @@ -47,29 +47,20 @@ function createRoundImg($imgpath) { } /** - * 高效判断远程图片是否存在 + * 判断远程文件是否存在 */ -function remoteImageExist($url) -{ - // $exif_imagetype = [ - // 1 => 'GIF', - // 2 => 'JPG', - // 3 => 'PNG', - // 4 => 'SWF', - // 5 => 'PSD', - // 6 => 'BMP', - // 7 => 'TIFF(intel byte order)', - // 8 => 'TIFF(motorola byte order)', - // 9 => 'JPC', - // 10 => 'JP2', - // 11 => 'JPX', - // 12 => 'JB2', - // 13 => 'SWC', - // 14 => 'IFF', - // 15 => 'WBMP', - // 16 => 'XBM' - // ]; - $isExsit = @exif_imagetype($url); - if ($isExsit === false) return false; - return in_array($isExsit, [1, 2, 3]) ? true : false; +function url_exists($url) { + $ch = curl_init(); + curl_setopt ($ch, CURLOPT_URL, $url); + //不下载 + curl_setopt($ch, CURLOPT_NOBODY, 1); + //设置超时 + curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 3); + curl_setopt($ch, CURLOPT_TIMEOUT, 3); + curl_exec($ch); + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + if($http_code == 200) { + return true; + } + return false; } \ No newline at end of file diff --git a/application/api/controller/User.php b/application/api/controller/User.php index 6535442..a3547b9 100644 --- a/application/api/controller/User.php +++ b/application/api/controller/User.php @@ -681,7 +681,7 @@ class User extends Api { $user = \app\api\model\User::get($this->auth->id); empty($user['avatar']) && $this->error('请先上传头像'); - !remoteImageExist($user['avatar']) && $this->error('头像失效,请更新头像'); + !url_exists($user['avatar']) && $this->error('头像失效,请更新头像'); // 本地路径 $dir = 'uploads/user'; if (!file_exists($dir)){