Jit*_*dav 34 deprecated mime-types php-5.5
我mime_content_type()在PHP 5.5中使用以获取MIME类型,但它会抛出fatal: error function not found.
如何在PHP 5.5上实现这一点?
Sha*_*ran 57
利用这些finfo()功能.
<?php
$finfo = finfo_open(FILEINFO_MIME_TYPE);
echo finfo_file($finfo, "path/to/image_dir/image.gif");
finfo_close($finfo);
Run Code Online (Sandbox Code Playgroud)
OUTPUT :
image/gif
Run Code Online (Sandbox Code Playgroud)
注意:Windows用户必须php_fileinfo.dll在php.ini中包含捆绑的DLL文件才能启用此扩展.
Eru*_*409 17
我花了太多时间试图让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");
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我想谈谈达乌兹的评论(因为它一直在起来投票),并提醒大家我在顶部放置伪免责声明,这不是"完全证明".因此,在考虑我在答案中提供的方法时,请记住这一点.
mime_content_type() 不被弃用,工作正常.
为什么PHP中不推荐使用mime_content_type()?
http://php.net/manual/en/function.mime-content-type.php
从PHP 5.3开始,它甚至内置.
| 归档时间: |
|
| 查看次数: |
68722 次 |
| 最近记录: |