這篇文章主要介紹php如何實(shí)現(xiàn)不通過(guò)擴(kuò)展名準(zhǔn)確判斷文件類型,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
第一種方法
通過(guò)php的finfo_file()
$handle=finfo_open(FILEINFO_MIME_TYPE);//This function opens a magic database and returns its resource. $fileInfo=finfo_file($handle,'./test.txt');// Return information about a file finfo_close($handle); print_r($fileInfo); echo '==========="\n"';
另外
finfo_buffer: Return information about a string buffer
finfo_close: Close fileinfo resource
mime_content_type:Detect MIME Content-type for a file (deprecated)
第二種方法
通過(guò)二進(jìn)制流獲取文件內(nèi)容
$fp=fopen('test.txt','r')///實(shí)際是image/png $bin = fread($fp, 2); //只讀2字節(jié) fclose($fp); $str_info = @unpack(“C2chars”, $bin);//Unpack data from binary string $type_code = intval($str_info['chars1'].$str_info['chars2']);// Get the integer value of a variable $file_type = ”; switch ($type_code) { case 7790: $file_type = 'exe'; break; case 7784: $file_type = 'midi'; break; case 8075: $file_type = 'zip'; break; case 8297: $file_type = 'rar'; break; case 255216: $file_type = 'jpg'; break; case 7173: $file_type = 'gif'; break; case 6677: $file_type = 'bmp'; break; case 13780: $file_type = 'png'; break; default: $file_type = 'unknown'; break; }
以上是“php如何實(shí)現(xiàn)不通過(guò)擴(kuò)展名準(zhǔn)確判斷文件類型”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!