すぐに思いつく解決策は 2 つあります。
1) コマンドで使用grep
するexec
(サーバーがサポートしている場合のみ):
$query = $_GET['string'];
$found = array();
exec("grep -Ril '" . escapeshellarg($query) . "' " . $_SERVER['DOCUMENT_ROOT'], $found);
完了すると、クエリを含むすべてのファイル パスが に配置され$found
ます。この配列を繰り返し処理し、必要に応じて処理/表示できます。
2) フォルダーを再帰的にループして各ファイルを開き、文字列を検索して、見つかった場合は保存します。
function search($file, $query, &$found) {
if (is_file($file)) {
$contents = file_get_contents($file);
if (strpos($contents, $query) !== false) {
// file contains the query string
$found[] = $file;
}
} else {
// file is a directory
$base_dir = $file;
$dh = opendir($base_dir);
while (($file = readdir($dh))) {
if (($file != '.') && ($file != '..')) {
// call search() on the found file/directory
search($base_dir . '/' . $file, $query, $found);
}
}
closedir($dh);
}
}
$query = $_GET['string'];
$found = array();
search($_SERVER['DOCUMENT_ROOT'], $query, $found);
これは、要求された文字列の各サブフォルダー/ファイルを (未テストで) 再帰的に検索する必要があります。見つかった場合は、変数に格納されます$found
。