一个强大的文件类型检测函数,它可以判断你上传的文件是什么类型,这个你没有办法为装过去的,有需要的朋友可以参考一下。
代码如下 | 复制代码 |
** * * * @access public * @param string filename 文件名 * @param string limit_ext_types 允许的文件类型,用|包围的类型如:|gif|txt| * @return string */ function check_file_type($filename, $limit_ext_types = ''){ $extname = strtolower(substr($filename, strrpos($filename, '.') + 1)); if ($limit_ext_types && stristr($limit_ext_types, '|' . $extname . '|') === false){ return ''; } $str = $format = ''; $file = @fopen($filename, 'rb'); if ($file){ $str = @fread($file, 0x400); // 读取前 1024 个字节 @fclose($file); } else{ $format=$extname; } if ($format == '' && strlen($str) >= 2 ){ if (substr($str, 0, 4) == 'MThd' && $extname != 'txt'){ $format = 'mid'; } elseif (substr($str, 0, 4) == 'RIFF' && $extname == 'wav'){ $format = 'wav'; } elseif (substr($str ,0, 3) == "xFFxD8xFF"){ $format = 'jpg'; } elseif (substr($str ,0, 4) == 'GIF8' && $extname != 'txt'){ $format = 'gif'; } elseif (substr($str ,0, 8) == "x89x50x4Ex47x0Dx0Ax1Ax0A"){ $format = 'png'; } elseif (substr($str ,0, 2) == 'BM' && $extname != 'txt'){ $format = 'bmp'; } elseif ((substr($str ,0, 3) == 'CWS' || substr($str ,0, 3) == 'FWS') && $extname != 'txt'){ $format = 'swf'; } elseif (substr($str ,0, 4) == "xD0xCFx11xE0"){ // D0CF11E == DOCFILE == Microsoft Office Document if (substr($str,0x200,4) == "xECxA5xC1x00" || $extname == 'doc'){ $format = 'doc'; } elseif (substr($str,0x200,2) == "x09x08" || $extname == 'xls'){ $format = 'xls'; } elseif (substr($str,0x200,4) == "xFDxFFxFFxFF" || $extname == 'ppt'){ $format = 'ppt'; } } elseif (substr($str ,0, 4) == "PKx03x04"){ $format = 'zip'; } elseif (substr($str ,0, 4) == 'Rar!' && $extname != 'txt'){ $format = 'rar'; } elseif (substr($str ,0, 4) == "x25PDF"){ $format = 'pdf'; } elseif (substr($str ,0, 3) == "x30x82x0A"){ $format = 'cert'; } elseif (substr($str ,0, 4) == 'ITSF' && $extname != 'txt'){ $format = 'chm'; } elseif (substr($str ,0, 4) == "x2ERMF"){ $format = 'rm'; } elseif ($extname == 'sql'){ $format = 'sql'; } elseif ($extname == 'txt'){ $format = 'txt'; } } if ($limit_ext_types && stristr($limit_ext_types, '|' . $format . '|') === false){ $format = ''; } return $format; } |