这篇文章将为大家详细讲解有关通过文件头准确识别PHP上传的文件类型有哪些,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
<?php $filename = '22.jpg'; $extname = strtolower(substr($filename, strrpos($filename, '.') + 1)); echo $extname.'<br />'; $file = @fopen($filename, 'rb'); if ($file) { $str = @fread($file, 0x400); // 读取前 1024 个字节 echo substr($str, 0, 4); @fclose($file); } 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) == "/xFF/xD8/xFF") { $format = 'jpg'; } elseif (substr($str ,0, 4) == 'GIF8' && $extname != 'txt') { $format = 'gif'; } elseif (substr($str ,0, 8 ) == "/x89/x50/x4E/x47/x0D/x0A/x1A/x0A") { $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) == "/xD0/xCF/x11/xE0") { // D0CF11E == DOCFILE == Microsoft Office Document if (substr($str,0x200,4) == "/xEC/xA5/xC1/x00" || $extname == 'doc') { $format = 'doc'; } elseif (substr($str,0x200,2) == "/x09/x08" || $extname == 'xls') { $format = 'xls'; } elseif (substr($str,0x200,4) == "/xFD/xFF/xFF/xFF" || $extname == 'ppt') { $format = 'ppt'; } } elseif (substr($str ,0, 4) == "PK/x03/x04") { $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) == "/x30/x82/x0A") { $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'; } echo $format;
关于通过文件头准确识别PHP上传的文件类型有哪些就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。