对于PHP <5.3,请使用mime_content_type()对于PHP> = 5.3,请使用finfo_open()或mime_content_type()
获得MimeType的替代方法是exif_imagetype和getimagesize,但是这些方法依赖于安装适当的库。此外,它们可能仅返回图像模仿类型,而不是magic.mime中给出的整个列表。
虽然mime_content_type
可以从PHP 4.3获得并且是FileInfo扩展的一部分(自PHP 5.3起默认启用,但Windows平台除外,该平台必须手动启用
如果您不想打扰系统上可用的功能,只需将所有四个函数包装到一个代理方法中,该方法将函数调用委托给任何可用的方法,例如
function getMimeType($filename)
{
$mimetype = false;
if(function_exists('finfo_open')) {
// open with FileInfo
} elseif(function_exists('getimagesize')) {
// open with GD
} elseif(function_exists('exif_imagetype')) {
// open with EXIF
} elseif(function_exists('mime_content_type')) {
$mimetype = mime_content_type($filename);
}
return $mimetype;
}