您的位置:首页 > 编程语言 > PHP开发

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;

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php 文件搜索