简体   繁体   English

如何获取PHP <5.3的文件内容类型?

[英]How to get file content-type with PHP < 5.3?

How do I retrieve the content-type of a file using PHP? 如何使用PHP检索文件的内容类型?

I don't want to rely on the file extension alone. 我不想单独依赖文件扩展名。

Thanks! 谢谢!

http://www.php.net/manual/en/function.mime-content-type.php已被弃用到PECL Fileinfo扩展名。

Because files themselves don't necessarily store the mime-type, you have to use a variety of methods to get hints to what the correct type is. 因为文件本身不一定存储mime类型,所以必须使用各种方法来获取正确类型的提示。 If you know the file is an image, use http://www.php.net/manual/en/function.getimagesize.php ... and you can check if a .doc or .xsl "looks like" what the extension claims it to be, because those file types always start with the same set of bytes http://filext.com/file-extension/DOC (see "identifying characters"). 如果您知道该文件是图像,请使用http://www.php.net/manual/en/function.getimagesize.php ...并且您可以检查.doc或.xsl是否“看起来像”扩展名是什么声称它是,因为那些文件类型总是以相同的字节集开始http://filext.com/file-extension/DOC (参见“识别字符”)。 In some cases it would be very difficult to determine without relying on an extension (for instance what's the difference between text/plain and text/css apart from the .css, unless you were to parse the file and account for every possible text type?) 在某些情况下,如果不依赖扩展名就很难确定(例如,除了.css之外,text / plain和text / css之间的区别是什么,除非你要解析文件和帐户的每种可能的文本类型? )

In the end, the more you know about the file types you're interested in, the more you can use that to verify a file to be of that type, and the more you spend writing code to do that job, the better the chances of you determining the correct mime-type! 最后,您对自己感兴趣的文件类型了解得越多,就可以越多地使用它来验证该类型的文件,并且编写代码来完成这项工作的花费越多,机会就越大你确定正确的哑剧类型!

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

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