mime-types - 在 PHP 5.5中,不推荐获得文件的MIME类型?

  显示原文与译文双语对照的内容

我在 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);



OUTPUT :

 
image/gif



 

注意:Windows 用户必须在 php.ini 中包含捆绑的php_fileinfo.dll DLL文件以启用这里扩展。

原作者:

我花了太多的时间来尝试让finfo函数正常工作。 最后我最终创建了自己的函数来匹配任何mime类型的array 文件扩展名。 确保文件是真正的扩展,这不是完全证明的方法,但是如何处理 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");


 }



}



编辑:

我们希望在评论时注意到我提供的方法,请记住我提供的答案。

...