我正在使用PHP 5.5中的mime_content_type()
函数获取MIME类型,但它会报错:fatal: error function not found
。
在PHP 5.5上如何实现这个功能?
我正在使用PHP 5.5中的mime_content_type()
函数获取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);
输出:
image/gif
注意:Windows用户必须在php.ini文件中包含捆绑的php_fileinfo.dll
DLL文件才能启用此扩展。
if($_SERVER['REQUEST_METHOD']=='POST'){
echo $_FILES['file']['name'].'
';
$finfo = finfo_open(FILEINFO_MIME_TYPE);
echo finfo_file($finfo, $_FILES['file']['name']);
finfo_close($finfo);
}
它显示了文件名,但没有Mime类型。 :) - Jitendra Yadav我花了太多时间试图让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的评论(因为它一直被点赞),并提醒大家,我在顶部放置了伪免责声明,说明这不是“万无一失”的。因此,在考虑我所提供的方法时,请记住这一点。
$extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
,这样它也可以检测到具有大写扩展名的文件的 MIME 类型。 - m132mime_content_type()
并未被弃用并且能够正常工作。
为什么 PHP 中的 mime_content_type() 函数被弃用?
http://php.net/manual/en/function.mime-content-type.php
自 PHP 5.3 开始,这个函数甚至是内置的。
fileinfo
扩展才能使其工作!(您所提到的页面只是说您可以重新编译扩展而不需要mime_magic
库。) - Sz.$finfo = finfo_open(FILEINFO_MIME_TYPE);
应该可以解决问题。
引用自php.net文档。您的函数已经被弃用,可能已经被移除。
;
)以下行:;extension=php_fileinfo.dll
。 - jave.web$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警告。
如果文件信息未正确安装,并且您使用的是最新版本的PHP,则可以从标头中获取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'];
使用以下代码获取图片大小:
$infFil=getimagesize($the_file_name);
另外,你也可以使用下面的代码:
echo $infFil["mime"]
getimagesize
返回一个关联数组,其中包含 MIME 键和显然的图像大小。我使用它并且它有效。我使用来自Bat的MimeTypeTool(https://github.com/lingtalfi/Bat)
如果可用,它将使用fileinfo,并在否则默认返回“扩展名 => mime类型”映射。
这是我通过结合两篇非常好的文章找到的最佳解决方案
// 感谢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/zh/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 函数,它仍将继续工作。