3 回答
![?](http://img1.sycdn.imooc.com/54585050000156a302200220-100-100.jpg)
TA贡献1998条经验 获得超6个赞
获取模仿类型的本机方法:
对于PHP <5.3,请使用mime_content_type()
对于PHP> = 5.3,请使用finfo_fopen()
获得MimeType的替代方法是exif_imagetype和getimagesize,但是这些方法依赖于安装适当的库。此外,它们可能仅返回图像模仿类型,而不是magic.mime中给出的整个列表。
如果您不想打扰系统上可用的功能,只需将所有四个函数包装到一个代理方法中,该方法将函数调用委托给任何可用的方法,例如
function getMimeType($filename)
{
$mimetype = false;
if(function_exists('finfo_fopen')) {
// 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;
}
![?](http://img1.sycdn.imooc.com/5458471300017f3702200220-100-100.jpg)
TA贡献1794条经验 获得超7个赞
要查找文件的mime类型,请使用以下包装函数:
function Mime($path)
{
$result = false;
if (is_file($path) === true)
{
if (function_exists('finfo_open') === true)
{
$finfo = finfo_open(FILEINFO_MIME_TYPE);
if (is_resource($finfo) === true)
{
$result = finfo_file($finfo, $path);
}
finfo_close($finfo);
}
else if (function_exists('mime_content_type') === true)
{
$result = preg_replace('~^(.+);.*$~', '$1', mime_content_type($path));
}
else if (function_exists('exif_imagetype') === true)
{
$result = image_type_to_mime_type(exif_imagetype($path));
}
}
return $result;
}
- 3 回答
- 0 关注
- 625 浏览
添加回答
举报