This article mainly introduces relevant information about the detailed explanation of the example of PHP judging the file upload image format. I hope this article can help everyone implement this method. It is very valuable for reference. Friends in need can refer to it
php Detailed example of determining the file upload image format
Determine the file image type,
##
$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 = '只能上传图片类型格式'; } // if ($strInfo['chars1']=='-1' AND $strInfo['chars2']=='-40' ) return 'jpg'; // if ($strInfo['chars1']=='-119' AND $strInfo['chars2']=='80' ) return 'png'; return $fileType; }
The above is the detailed content of Introduction to the method of determining the image format of file upload in PHP. For more information, please follow other related articles on the PHP Chinese website!