如何使用PHP判断文件是否为文本? [英] How can I tell if a file is text using PHP?
问题描述
我正在为我们庞大的PHP代码库创建搜索引擎.
I'm making a search engine for our gigantic PHP codebase.
给出文件路径,如何确定文件是文本文件还是其他类型的文件?我宁愿不必求助于文件扩展名(例如substr($filename, -3)
或愚蠢的东西),因为这是基于linux的文件系统,因此就文件扩展名而言,一切都是如此.
Given a filepath, how can I determine with some degree of certainty whether a file is a text file, or some other type? I'd prefer not to have to resort to file extensions (like substr($filename, -3)
or something silly), as this is a linux based filesystem, so anything goes as far as file extensions are concerned.
我正在使用RecursiveDirectoryIterator,所以我也可以使用那些方法.
I'm using RecursiveDirectoryIterator, so I have those methods available too..
推荐答案
尝试 finfo_file()
函数.
这是一个描述其用法的博客:智能使用PHP进行文件类型检测
Here's a blog describing its usage: Smart File Type Detection Using PHP
这篇关于如何使用PHP判断文件是否为文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!