HTML Recursive File Search for a string

This function will search all files within a folder, recursively, for a search string.

This can be used for a rudimentary search feature.  Or, if your web host doesn't allow SSH and you have no access to grep.
function recursiveFileSearch($folder, $searchString, $extension = 'php') {

	$foundArray = array();
	$it = new RecursiveDirectoryIterator($folder);
	foreach(new RecursiveIteratorIterator($it) as $file) {
	    $contents = file_get_contents($file);
	    if(strpos($contents, $searchString) !== false) {
	        $foundArray[] = $file;
	if(count($foundArray)) {
		return $foundArray;
	else {
		return false;


$matched_files = recursiveFileSearch('/RELATIVE/OR/ABSOLUTE/FILE/PATH', 'YOUR_SEARCH_STRING');

print_r ($matched_files);
Posted by
Snippet Viewed 4952 times.

Share your HTML code snippets:

  • Get some recognition & link back to your site.


Submit | Browse

Most Recent html snippets

Most Viewed