简体   繁体   English

获取没有PECL扩展名的PHP5中本地文件的MIME类型?

[英]Get MIME type of a local file in PHP5 without a PECL extension?

mime_content_type() is deprecated. mime_content_type()已弃用。

How can I find the MIME type of a local file using PHP5 but without using this deprecated method or the PECL fileinfo extension? 如何使用PHP5查找本地文件的MIME type ,而不使用不推荐使用的方法或PECL fileinfo扩展名?

Edit: That's what I was afraid of. 编辑:那就是我所担心的。 It's unfortunate that they deprecated a built-in function in favour of one that requires an extension that isn't always available. 不幸的是,他们不赞成使用内置函数,而倾向于需要一个并不总是可用的扩展的函数。

If you can't use the fileinfo extension, and you don't want to use mime_content_type, your options are limited. 如果您不能使用fileinfo扩展名,并且不想使用mime_content_type,那么您的选择将受到限制。

Most likely you'll need to do a lookup based on the file extension. 最有可能需要根据文件扩展名进行查找。 mime_content_type did something a bit more intelligent and actually looked for special data in the file to determine the mime type. mime_content_type做了一些更智能的操作,实际上在文件中寻找特殊数据来确定mime类型。

The getID3() library is a quick and easy works-most-of-the-time option. getID3()库是最快捷,最便捷的时机选择。 Originally named for a project to obtain MP3 ID3 data, the library does two hecks of a lot more than that and is quite convenient for all sorts of common or odd file meta data tasks. 该库最初是为获得MP3 ID3数据的项目而命名的,它的功能远远超出了这两个范围,并且对于各种常见或奇数文件元数据任务非常方便。

I've used it to get the MIME types of files for online image and video tools. 我用它来获取在线图像和视频工具的文件的MIME类型。 In all the testing I've done I've not seen getID3 get the MIME type wrong. 在完成的所有测试中,我没有看到getID3弄错了MIME类型。

I've also used it to check if QuickTime videos have streaming hints. 我还用它来检查QuickTime视频是否有流媒体提示。 I mention this as an example of versatility. 我将其作为多功能性的一个例子。

A second more time consuming option is to roll your own MIME type checker as already suggested. 第二个比较耗时的选择是按照建议的方式滚动自己的MIME类型检查器。 If you have a MIME magic file you can go a little further than a lookup on the file extension by comparing the first n bytes of file data against a first-n-bytes to MIME type lookup table derived from your MIME magic file. 如果您有MIME魔术文件,则可以通过将文件数据的前n个字节与从MIME魔术文件派生的MIME类型查找表进行比较,将文件数据的前n个字节与前n个字节相比,可以对文件扩展名进行更多的查找。

A typical MIME magic file will contain in excess of 500 sets of MIME types which might result in slow comparisons (lots of checks to make). 一个典型的MIME魔术文件将包含超过500套MIME类型,这可能导致比较缓慢(要进行大量检查)。 Hard-coding the 10 most common MIME type checks in your home rolled solution will help there. 在您的家用解决方案中对10种最常见的MIME类型检查进行硬编码将对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM