网站首页 文章专栏 isImg($fileName)方法,判断是否为图片
方法一:
function isImg($fileName) {
$file = fopen($fileName, "rb");
$bin = fread($file, 2); // 只读2字节
fclose($file);
$strInfo = @unpack("C2chars", $bin);
$typeCode = intval($strInfo['chars1'] . $strInfo['chars2']);
$fileType = '';
if ($typeCode == 255216/*jpg*/ || $typeCode == 7173/*gif*/ || $typeCode == 13780/*png*/) {
return $typeCode;
} else {
// echo '"仅允许上传jpg/jpeg/gif/png格式的图片!';
return false;
}
方法二:
public function isImg($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //是否跟着爬取重定向的页面 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //将curl_exec()获取的值以文本流的形式返回,而不是直接输出。 curl_setopt($ch, CURLOPT_HEADER, 1); // 启用时会将头文件的信息作为数据流输出 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); //设置超时时间 curl_setopt($ch, CURLOPT_URL, $url); //设置URL $content = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); //curl的httpcode $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); //获取头大小 curl_close($ch); $img_type = explode("/", trim($headers_arr['Content-Type'])); //然后将获取到的Content-Type中的值用/分隔开 if ($httpcode == 200 && strcasecmp($img_type[0], 'image') == 0) { //如果httpcode为200,并且Content-type前面的部分为image,则说明该链接可以访问成功,并且是一个图片类型的 $type = $img_type[1]; return true; } else { return false; } }
转载请注明出处