当前位置: 移动技术网 > IT编程>开发语言>PHP > php判断文件上传图片格式的实例详解

php判断文件上传图片格式的实例详解

2017年10月07日  | 移动技术网IT编程  | 我要评论

sibt portal,肺热咳嗽,论文发表网站

php判断文件上传图片格式的实例详解

判断文件图片类型,

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

如上如果用户修改文件后缀为png jpeg等无法满足,查了查资料解决方法是采用判断文件的二进制流信息,如果你刚好遇到这种问题不妨尝试一下:

 //*判断图片上传格式是否为图片 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 = '只能上传图片类型格式';
  }
  // if ($strinfo['chars1']=='-1' and $strinfo['chars2']=='-40' ) return 'jpg';
  // if ($strinfo['chars1']=='-119' and $strinfo['chars2']=='80' ) return 'png';
  return $filetype;
 }

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网