判断文件图片类型,

$type  = $_FILES['image']['tmp_name'];//文件名
//$type  = $this->getImagetype( $type ); 
$filetype = ['jpg', 'jpeg', 'gif', 'bmp', 'png'];
if (! in_array($type, $filetype))
{ 
 return "不是图片类型";
}

//*判断图片上传格式是否为图片 return返回文件后缀
public function getImagetype($filename)
{
 $file = fopen($filename, 'rb');
 $bin = fread($file, 2); //只读2字节
 fclose($file);
 $strInfo = @unpack('C2chars', $bin);
 $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
 // dd($typeCode);
 $fileType = '';
 switch ($typeCode) {
  case 255216:
   $fileType = 'jpg';
   break;
  case 7173:
   $fileType = 'gif';
   break;
  case 6677:
   $fileType = 'bmp';
   break;
  case 13780:
   $fileType = 'png';
   break;
  default:
   $fileType = '只能上传图片类型格式';
 }
 return $fileType;
}

  

posted @ 2019-07-22 14:03  TOPHP  阅读(1847)  评论(0编辑  收藏  举报