您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)使用php怎么準(zhǔn)確的獲取文件的MIME類型,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
具體實(shí)現(xiàn)方法如下:
?<?php $mime = array ( //applications 'ai' => 'application/postscript', 'eps' => 'application/postscript', 'exe' => 'application/octet-stream', 'doc' => 'application/vnd.ms-word', 'xls' => 'application/vnd.ms-excel', 'ppt' => 'application/vnd.ms-powerpoint', 'pps' => 'application/vnd.ms-powerpoint', 'pdf' => 'application/pdf', 'xml' => 'application/xml', 'odt' => 'application/vnd.oasis.opendocument.text', 'swf' => 'application/x-shockwave-flash', // archives 'gz' => 'application/x-gzip', 'tgz' => 'application/x-gzip', 'bz' => 'application/x-bzip2', 'bz2' => 'application/x-bzip2', 'tbz' => 'application/x-bzip2', 'zip' => 'application/zip', 'rar' => 'application/x-rar', 'tar' => 'application/x-tar', '7z' => 'application/x-7z-compressed', // texts 'txt' => 'text/plain', 'php' => 'text/x-php', 'html' => 'text/html', 'htm' => 'text/html', 'js' => 'text/javascript', 'css' => 'text/css', 'rtf' => 'text/rtf', 'rtfd' => 'text/rtfd', 'py' => 'text/x-python', 'java' => 'text/x-java-source', 'rb' => 'text/x-ruby', 'sh' => 'text/x-shellscript', 'pl' => 'text/x-perl', 'sql' => 'text/x-sql', // images 'bmp' => 'image/x-ms-bmp', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png', 'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'tga' => 'image/x-targa', 'psd' => 'image/vnd.adobe.photoshop', //audio 'mp3' => 'audio/mpeg', 'mid' => 'audio/midi', 'ogg' => 'audio/ogg', 'mp4a' => 'audio/mp4', 'wav' => 'audio/wav', 'wma' => 'audio/x-ms-wma', // video 'avi' => 'video/x-msvideo', 'dv' => 'video/x-dv', 'mp4' => 'video/mp4', 'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg', 'mov' => 'video/quicktime', 'wm' => 'video/x-ms-wmv', 'flv' => 'video/x-flv', 'mkv' => 'video/x-matroska' ); function _getMimeDetect() { if (class_exists('finfo')) { return 'finfo'; } else if (function_exists('mime_content_type')) { return 'mime_content_type'; } else if ( function_exists('exec')) { $result = exec('file -ib '.escapeshellarg(__FILE__)); if ( 0 === strpos($result, 'text/x-php') OR 0 === strpos($result, 'text/x-c++')) { return 'linux'; } $result = exec('file -Ib '.escapeshellarg(__FILE__)); if ( 0 === strpos($result, 'text/x-php') OR 0 === strpos($result, 'text/x-c++')) { return 'bsd'; } } return 'internal'; } function _getMimeType($path) { global $mime; $fmime = _getMimeDetect(); switch($fmime) { case 'finfo': $finfo = finfo_open(FILEINFO_MIME); if ($finfo) $type = @finfo_file($finfo, $path); break; case 'mime_content_type': $type = mime_content_type($path); break; case 'linux': $type = exec('file -ib '.escapeshellarg($path)); break; case 'bsd': $type = exec('file -Ib '.escapeshellarg($path)); break; default: $pinfo = pathinfo($path); $ext = isset($pinfo['extension']) ? strtolower($pinfo['extension']) : ''; $type = isset($mime[$ext]) ? $mime[$ext] : 'unkown'; break; } $type = explode(';', $type); //需要加上這段,因?yàn)槿绻褂胢ime_content_type函數(shù)來(lái)獲取一個(gè)不存在的$path時(shí)會(huì)返回'application/octet-stream' if ($fmime != 'internal' AND $type[0] == 'application/octet-stream') { $pinfo = pathinfo($path); $ext = isset($pinfo['extension']) ? strtolower($pinfo['extension']) : ''; if (!empty($ext) AND !empty($mime[$ext])) { $type[0] = $mime[$ext]; } } return $type[0]; } $path = '1.txt'; //實(shí)際上當(dāng)前路徑并不存在1.txt var_dump(_getMimeType($path)); /*End of php*/
關(guān)于使用php怎么準(zhǔn)確的獲取文件的MIME類型就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。