我mime_content_type()
在 PHP 5.5 中使用来获取 MIME 类型,但它会抛出fatal: error function not found
.
如何在 PHP 5.5 上实现这一点?
我mime_content_type()
在 PHP 5.5 中使用来获取 MIME 类型,但它会抛出fatal: error function not found
.
如何在 PHP 5.5 上实现这一点?
充分利用这些finfo()
功能。
<?php
$finfo = finfo_open(FILEINFO_MIME_TYPE);
echo finfo_file($finfo, "path/to/image_dir/image.gif");
finfo_close($finfo);
OUTPUT :
image/gif
注意:Windows 用户必须在 php.ini 中包含捆绑的php_fileinfo.dll
DLL 文件才能启用此扩展。
我花了太多时间试图让 finfo 函数正常工作。我终于创建了自己的函数来将文件扩展名与任何 mime 类型数组匹配。这不是确保文件确实是扩展名所表示的文件的完全证明方式,但是可以通过在服务器上处理所述文件的 I/O 来缓解该问题。
function mime_type($file) {
// there's a bug that doesn't properly detect
// the mime type of css files
// https://bugs.php.net/bug.php?id=53035
// so the following is used, instead
// src: http://www.freeformatter.com/mime-types-list.html#mime-types-list
$mime_type = array(
"3dml" => "text/vnd.in3d.3dml",
"3g2" => "video/3gpp2",
"3gp" => "video/3gpp",
"7z" => "application/x-7z-compressed",
"aab" => "application/x-authorware-bin",
"aac" => "audio/x-aac",
"aam" => "application/x-authorware-map",
"aas" => "application/x-authorware-seg",
"abw" => "application/x-abiword",
"ac" => "application/pkix-attr-cert",
"acc" => "application/vnd.americandynamics.acc",
"ace" => "application/x-ace-compressed",
"acu" => "application/vnd.acucobol",
"adp" => "audio/adpcm",
"aep" => "application/vnd.audiograph",
"afp" => "application/vnd.ibm.modcap",
"ahead" => "application/vnd.ahead.space",
"ai" => "application/postscript",
"aif" => "audio/x-aiff",
"air" => "application/vnd.adobe.air-application-installer-package+zip",
"ait" => "application/vnd.dvb.ait",
"ami" => "application/vnd.amiga.ami",
"apk" => "application/vnd.android.package-archive",
"application" => "application/x-ms-application",
// etc...
// truncated due to Stack Overflow's character limit in posts
);
$extension = \strtolower(\pathinfo($file, \PATHINFO_EXTENSION));
if (isset($mime_type[$extension])) {
return $mime_type[$extension];
} else {
throw new \Exception("Unknown file type");
}
}
编辑:
我想解决 Davuz 的评论(因为它不断得到投票)并提醒大家,我在顶部放置了伪免责声明,这不是“完全证明”。因此,在考虑我在回答中提供的方法时,请记住这一点。
mime_content_type()
没有被弃用并且工作正常。
为什么在 PHP 中不推荐使用 mime_content_type()?
http://php.net/manual/en/function.mime-content-type.php
从 PHP 5.3 开始,它甚至是内置的.
$finfo = finfo_open(FILEINFO_MIME_TYPE);
应该这样做。
取自 php.net 文档。您的功能已被弃用,可能已被删除。
使用以下方法获取图像大小:
$infFil=getimagesize($the_file_name);
和
echo $infFil["mime"]
getimagesize
返回一个关联数组,它有一个 MIME 键,显然还有图像大小
我用过它并且有效
您应该了解file_get_contents会将整个文件上传到内存,仅获取 mime 类型不是好方法。在这种情况下,您不需要使用缓冲区方法和file_get_contents函数。
为了防止任何错误和警告,最好这样做。
$filename = 'path to your file';
if (class_exists('finfo')) {
$finfo = new finfo(FILEINFO_MIME_TYPE);
if (is_object($finfo)) {
echo $finfo->file($filename);
}
} else {
echo 'fileinfo did not installed';
}
你也应该知道$finfo->file如果失败会抛出 PHP 警告。
如果 fileinfo 没有正确安装,并且你有一个新版本的 PHP,你可以从 headers 中获取 mime 类型。
您可以使用 cURL 从标题中获取 mime 类型。
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_HEADER => true,
CURLOPT_NOBODY => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 1,
CURLOPT_URL => $link)
);
$headers = curl_exec($ch);
curl_close($ch);
if (preg_match('/Content-Type:\s(.*)/i', $headers, $matches)) {
echo trim($matches[1], "\t\n\r");
}else {
echo 'There is no content type in the headers!';
}
您也可以使用get_headers函数,但它比 cURL 请求慢。
$url = 'http://www.example.com';
$headers = get_headers($url, 1);
echo $headers['Content-Type'];
我使用 Bat ( https://github.com/lingtalfi/Bat )的 MimeTypeTool
如果可用,它使用 fileinfo,否则默认返回“extension => mime type”映射。
这是我通过结合两个非常好的帖子找到的最佳解决方案
// 感谢http://php.net/manual/en/function.mime-content-type.php#87856
function getMimeContentType($filename, $ext)
{
if(!function_exists('mime_content_type'))
{
if($mime_types = getMimeTypes())
{
if (array_key_exists($ext, $mime_types))
{
return $mime_types[$ext];
}
elseif (function_exists('finfo_open'))
{
$finfo = finfo_open(FILEINFO_MIME);
$mimetype = finfo_file($finfo, $filename);
finfo_close($finfo);
return $mimetype;
}
}
return 'application/octet-stream';
}
return mime_content_type($filename);
}
// 感谢http://php.net/manual/en/function.mime-content-type.php#107798
function getMimeTypes()
{
$url = 'http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types';
$mimes = array();
foreach(@explode("\n",@file_get_contents($url)) as $x)
{
if(isset($x[0]) && $x[0]!=='#' && preg_match_all('#([^\s]+)#', $x, $out) && isset($out[1]) && ($c = count($out[1])) > 1)
{
for($i=1; $i < $c; $i++)
{
$mimes[$out[1][$i]] = $out[1][0];
}
}
}
return (@sort($mimes)) ? $mimes : false;
}
使用它链接这个:
$filename = '/path/to/the/file.pdf';
$ext = strtolower(array_pop(explode('.',$filename)));
$content_type = getMimeContentType($filename, $ext);
即使在 php 中不再支持 mime_content_type 函数,也将继续工作。