Sử dụng ở trên đơn giản [bạn có thể nói hàm lớn hơn 🤪], bạn có thể trích xuất hoặc lấy loại MIME của một tệp hoặc bạn có thể nói nội dung.function, you can extract or get the mime type of a file or you can say content.
Nhưng trước đây, để sử dụng chức năng này, bạn có thể cần phải thực hiện một số cấu hình trước,
Giống như bạn phải chắc chắn rằng bạn đã chuyển hoặc cấu hình tiện ích mở rộng Curl, FileSystem đã tái lập tiện ích mở rộng và tiện ích mở rộng FinFO trong tệp php.ini.
Ở đây, tôi đang mô tả toàn bộ quá trình của chức năng này trong một ngắn.
- Đầu tiên, chúng tôi đang lưu trữ tất cả các loại MIME được cập nhật dưới dạng một mảng từ URL loại Apache MIME chính thức., we are storing all the updated mime type as an array from official apache mime type url.
Bạn cũng có thể nhận được tệp loại MIME này trong thư mục Apache Conf của bạn đã sử dụng URL. Trong chức năng này, chúng tôi đang sử dụng URL trực tiếp để có được tất cả các loại MIME.
Nhưng quy trình Zeroth cho chức năng này là xác nhận rằng URL Apache có trực tiếp hay không. for this function is to validate that apache url is live or not.
Sau khi xác thực URL, nếu URL được xác thực [có nghĩa là trực tiếp], chúng tôi lưu trữ tất cả các mimes từ url đó dưới dạng một mảng có tên $ mimes$mimes
Nếu URL không sống hoặc tồn tại, chúng tôi sẽ tạo một mảng với một số tiện ích mở rộng chung có sẵn.
Sau đó, chúng tôi xác nhận nội dung là tệp.
Sau đó, chúng tôi kiểm tra chức năng PHP pathInfo để đảm bảo rằng có bất kỳ phần mở rộng tệp nào hay không. Nếu ở đó, lưu trữ nó.
Sau đó, chúng tôi kiểm tra mảng $ mimes với tiện ích mở rộng nội dung của chúng tôi dưới dạng chỉ mục mảng $ mimes.$mimes array with our content extension as $mimes array index.
Cuối cùng, chúng tôi đang trả về giá trị chỉ mục của mảng $ mimes dưới dạng loại mime nội dung thông qua biến $ content_mime. we are returning the index value of $mimes array as content mime type through $content_mime variable.
Đó là nó.
<?php /** * **get_content_mime_type * * @param string $content, the content or the file whose mime type you want to know. * @return string */ function get_content_mime_type($content) { $url = '//svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types'; $url_live = false; $handle = curl_init($url); curl_setopt_array($handle, array( CURLOPT_FOLLOWLOCATION => true, CURLOPT_NOBODY => true, CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false )); $response = curl_exec($handle); $httpCode = curl_getinfo($handle, CURLINFO_EFFECTIVE_URL); $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); if ($httpCode == 200) { $url_live = true; } $url_live = $url_live; curl_close($handle); $mimes = array(); if ($url_live) { $mimes_file = file_get_contents($url); preg_match_all('#^([^\s]{2,}?)\s+(.+?)$#ism', $mimes_file, $matches, PREG_SET_ORDER); foreach ($matches as $match) { $exts = explode(" ", $match[2]); foreach ($exts as $ext) { $mimes[$ext] = $match[1]; } } } else { $mimes = array( 'txt' => 'text/plain', 'htm' => 'text/html', 'html' => 'text/html', 'php' => 'text/html', 'css' => 'text/css', 'js' => 'application/javascript', 'json' => 'application/json', 'xml' => 'application/xml', 'swf' => 'application/x-shockwave-flash', 'flv' => 'video/x-flv', // images 'png' => 'image/png', 'jpe' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg', 'gif' => 'image/gif', 'bmp' => 'image/bmp', 'ico' => 'image/vnd.microsoft.icon', 'tiff' => 'image/tiff', 'tif' => 'image/tiff', 'svg' => 'image/svg+xml', 'svgz' => 'image/svg+xml', // archives 'zip' => 'application/zip', 'rar' => 'application/x-rar-compressed', 'exe' => 'application/x-msdownload', 'msi' => 'application/x-msdownload', 'cab' => 'application/vnd.ms-cab-compressed', // audio/video 'mp3' => 'audio/mpeg', 'qt' => 'video/quicktime', 'mov' => 'video/quicktime', // adobe 'pdf' => 'application/pdf', 'psd' => 'image/vnd.adobe.photoshop', 'ai' => 'application/postscript', 'eps' => 'application/postscript', 'ps' => 'application/postscript', // ms office 'doc' => 'application/msword', 'rtf' => 'application/rtf', 'xls' => 'application/vnd.ms-excel', 'ppt' => 'application/vnd.ms-powerpoint', 'docx' => 'application/msword', 'xlsx' => 'application/vnd.ms-excel', 'pptx' => 'application/vnd.ms-powerpoint', // open office 'odt' => 'application/vnd.oasis.opendocument.text', 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', ); } $content_mime = 'unknown'; if (is_file($content)) { if (isset(pathinfo($content) ['extension'])) { $content_ext = pathinfo($content) ['extension']; if (isset($mimes[$content_ext])) { $content_mime = $mimes[$content_ext]; } else { if (is_readable($content) && is_executable($content)) { $finfo = finfo_open(FILEINFO_MIME_TYPE); $content_mime = finfo_file($finfo, $content); if ($content_mime === null | $content_mime === "") { $content_mime = "application/octet-stream"; } else { $content_mime = $content_mime; } finfo_close($finfo); } else { $content_mime = "application/octet-stream"; } } } } else { // return whatever you want // $content_mime = 'unknown'; } $content_mime = $content_mime; return $content_mime; } ?>