使用上述简单的[你可以说更大的]
函数,您可以提取或获取文件或内容的MIME类型。
但是,在使用此功能之前,您可能需要完成一些预配置,
像您必须确保已在php.ini文件中打开或配置了curl扩展、文件系统相关扩展和finfo扩展。
这里,我将简要描述这个函数的整个过程。
1.首先,我们将所有更新的mime类型存储为一个数组,从官方apache mime类型网址中获取。
您也可以在apache conf目录中获取此mime类型文件,而不是使用url。在此函数中,我们使用实时url获取所有mime类型。
0.但是,这个函数的第零步骤是验证apache url是否正常。
验证url后,如果url被验证[意味着活动的],我们将所有mimes从该url存储为一个名为$mimes的数组。
如果URL不存在或无法访问,我们将手动创建一个包含一些常见扩展名的数组。
然后,我们会验证内容是否为文件。
接下来,我们使用PHP的pathinfo函数检查是否有文件扩展名。如果有,就将其存储下来。
在此之后,我们会用我们的内容扩展名作为$mimes数组的索引,检查$mimes数组。
最后,我们通过$content_mime变量以$mimes数组的索引值作为内容MIME类型返回。
就是这样。
<?php
function get_content_mime_type($content)
{
$url = 'http://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',
'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',
'zip' => 'application/zip',
'rar' => 'application/x-rar-compressed',
'exe' => 'application/x-msdownload',
'msi' => 'application/x-msdownload',
'cab' => 'application/vnd.ms-cab-compressed',
'mp3' => 'audio/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'pdf' => 'application/pdf',
'psd' => 'image/vnd.adobe.photoshop',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
'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',
'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
{
}
$content_mime = $content_mime;
return $content_mime;
}
?>
$result = new finfo();print $result->file($filename, FILEINFO_MIME_TYPE);
- Martin