您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)通過文件頭準(zhǔn)確識(shí)別PHP上傳的文件類型有哪些,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
<?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 個(gè)字節(jié) 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;
關(guān)于通過文件頭準(zhǔn)確識(shí)別PHP上傳的文件類型有哪些就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。