需求: 寫了大量的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;
}