[英]How to categorize Mime Types (PHP)
I'm looking for a code snippet which categorizes mime types. 我正在寻找一个代码片段,它对mime类型进行分类。
For example, 例如,
application/msword 应用程序/ msword
application/vnd.oasis.opendocument.text 应用程序/ vnd.oasis.opendocument.text
application/pdf 应用程序/ PDF格式
Both of them are office files. 它们都是办公室文件。 When I pass these mime type to the function, I want it to return a result which is 'office', 'image', 'application', 'compressed' etc.
当我将这些mime类型传递给函数时,我希望它返回“office”,“image”,“application”,“compressed”等结果。
However,as you know, there are hundreds of mime types and I can not collect all of them. 但是,如你所知,有数百种mime类型,我无法收集所有这些类型。
Do you know where can I find it? 你知道我在哪里可以找到它吗?
I don't know of any existing script that would classify these types as you wish. 我不知道任何现有的脚本会根据您的意愿对这些类型进行分类。 You may need to create this function yourself based on the distinctions you require, eg application/msword -> office rather than application, etc.
您可能需要根据所需的区别自行创建此功能,例如application / msword - > office而不是application等。
As your classifications are fairly arbitrary and particular to your own use-case, you'll likely have to classify them yourself into your desired categories, eg by using a function something like the below: 由于您的分类相当随意并且特定于您自己的用例,您可能必须将它们自己分类到您想要的类别中,例如通过使用如下所示的函数:
/**
* Classify mime types into pre-determined categories
* 2-d array used for simplicity of example, error
* checking omitted so unrecognised string returns
* empty value here...
*/
function categorize_mime_types($mime)
{
// Classify mime types into desired categories, key-val pairings
$mimes = array("application/msword"=>"office",
"application/vnd.oasis.opendocument.text"=>"office",
....
"image/jpeg"=>"image");
return $mimes[$mime];
}
Collecting all of the mime types to do this could be quite time-consuming, although there are a number of website which have large lists to get you started. 收集所有mime类型来执行此操作可能非常耗时,尽管有许多网站都有大型列表可以帮助您入门。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.