PHP 查找某类型文件包含特定字符
2017-02-13 09:55
441 查看
需求: 写了大量的php程序,突然想找回某一个文件(含特定关键字),翻遍了虚拟目录里面的每一个php文件都没有找到,于是写了个脚本去帮忙搜索
// 例子
$fileList = array();
$dir = "E:";
$ext =".php";
$word="Keyword";
findWord($dir,$ext,$word);
/*
功能:遍历文件夹指定类型文件,找到含某关键字文件
输入:$dir 目录 , $ext 文件后缀, $word 关键字
输出:文件List
*/
function findWord($dir,$ext,$word){
$rList = array();
$list = read_all_dir ( $dir,$ext );
foreach($list as $file){
$c = file_get_contents($file);
if(strpos($c,$word)!==false){
$rList[]=$file;
}
}
print_r($rList);
}
/*
遍历某个目录下的所有文件和子文件夹,返回某个后缀的文件列表;
*/
function read_all_dir ( $dir )
{
global $fileList,$ext;
$result = array();
$handle = opendir($dir);
if ( $handle )
{
while ( ( $file = readdir ( $handle ) ) !== false )
{
if ( $file != '.' && $file != '..')
{
$cur_path = $dir . DIRECTORY_SEPARATOR . $file;
if ( is_dir ( $cur_path ) )
{
$result['dir'][$cur_path] = read_all_dir ( $cur_path );
}
else
{
$result['file'][] = $cur_path;
if(strpos($cur_path,$ext)!==false){
$fileList[]= $cur_path;
}
}
}
}
closedir($handle);
}
//return $result;
return $fileList;
}
// 例子
$fileList = array();
$dir = "E:";
$ext =".php";
$word="Keyword";
findWord($dir,$ext,$word);
/*
功能:遍历文件夹指定类型文件,找到含某关键字文件
输入:$dir 目录 , $ext 文件后缀, $word 关键字
输出:文件List
*/
function findWord($dir,$ext,$word){
$rList = array();
$list = read_all_dir ( $dir,$ext );
foreach($list as $file){
$c = file_get_contents($file);
if(strpos($c,$word)!==false){
$rList[]=$file;
}
}
print_r($rList);
}
/*
遍历某个目录下的所有文件和子文件夹,返回某个后缀的文件列表;
*/
function read_all_dir ( $dir )
{
global $fileList,$ext;
$result = array();
$handle = opendir($dir);
if ( $handle )
{
while ( ( $file = readdir ( $handle ) ) !== false )
{
if ( $file != '.' && $file != '..')
{
$cur_path = $dir . DIRECTORY_SEPARATOR . $file;
if ( is_dir ( $cur_path ) )
{
$result['dir'][$cur_path] = read_all_dir ( $cur_path );
}
else
{
$result['file'][] = $cur_path;
if(strpos($cur_path,$ext)!==false){
$fileList[]= $cur_path;
}
}
}
}
closedir($handle);
}
//return $result;
return $fileList;
}
相关文章推荐
- ios文件下载实现(包含字符的反转和特定字符查找)
- 查找特定目录下包含特定字符的文件
- 查找特定目录下包含特定字符的文件
- 用 find 命令在指定文件类型中查找包含指定文字的文件
- 在指定文件夹中的文件中查找包含指定字符的行(这个小东西不错[Perl])
- grep在指定类型的文件中查找字符
- 【php】读文件找出特定字符
- grep在指定类型的文件中查找字符
- shell脚本(从一些文件中查找含有特定字符的文件,将转移到指定地方)
- Perl 在指定文件夹中的文件中查找包含指定字符的行
- php strstr查找字符串中是否包含某些字符的查找函数
- 查找特定文件类型,特定用户文件然后解压缩
- Solaris下用find查找文件中的特定字符窜
- linux查找文件命令 要查找包含某字符
- 查找当前目录下包含特定字符串的特殊后缀名的文件
- linux 下查找 包含特定内容的文件 或包含特定文件名的文件
- linux 查找包含指定字符的文件列表 命令
- 查找包含特定字符的存储过程
- php中include包含文件路径查找过程