1

I'm trying to find out why this entire code below is producing only ONE result after running all of the functions, instead of producing multiple results.

The code is supposed to take a single result from a text-based search, and then check each file on the user's server and find that string, then output each file that contains that search string.

The code is producing only one result, instead of outputting all of the files that would contain the entered string.

I had this working at one time, but the code was modified from a prior question on this website, and now is not working exactly correct.

<?php

if (!isset($_REQUEST['query'])) 
{ 
    //Ask for query here :)      
    //echo "<p style=\"color:darkgray; font-family:arial\">Sorry. Please enter a search term.</p>"; 
    exit; 
} 

$query = isset($_REQUEST['query']) ? $_REQUEST['query'] : ''; 


if (empty($query)) 
{ 
    echo "<p style=\"color:#575757; font-family:arial\">We are unable to process your request becuase you didn't enter a search term. Please try again.</p>"; 
    exit; 
} 


$filesFound = find_files('.'); 

if (!$filesFound) 
{ 
    echo "<p style=\"color:#575757; font-family:arial\">No files contained the search, \"$query\". Please try another search.</p>"; 
} 


function find_files($seed) 
{ 
    if (!is_dir($seed)) return false; 
    $found = false; 
    $dirs = array($seed); 

    while (NULL !== ($dir = array_pop($dirs))) 
    { 
        if ($dh = opendir($dir)) 
        { 
            while (false !== ($file = readdir($dh))) 
            { 
                if ($file == '.' || $file == '..') continue; 
                $path = $dir . '/' . $file; 
                if (is_dir($path)) 
                { 
                    $dirs[] = $path; 
                } 
                else 
                { 
                    if (preg_match('/^.*\.(php[\d]?|js|txt)$/i', $path)) 
                    { 
                        if (!$found) 
                        { 
                            $found = check_files($path); 
                        } 
                    } 
                } 
            } 
            closedir($dh); 
        } 
    } 
    return $found; 
} 

function check_files($this_file) 
{ 
    $query = $_REQUEST['query']; 

    $str_to_find = $query; 


    if (($content = file_get_contents($this_file)) === false) 
    { 
        echo("<p style=\"color:#575757; font-family:arial\">Could not check $this_file</p>\n"); 
        return false; 
    } 
    else 
    { 
        if (stristr($content, $str_to_find)) 
        { 
            echo("<p style=\"color:#575757; font-family:arial\">$this_file -> contains $str_to_find</p>\n"); 
            return true; 
        } 
    } 
} 

?>
0

1 Answer 1

1
if (!$found) 
{ 
    $found = check_files($path); 
} 

This if clause allows only one file to be found. Replace it with

if (check_files($path))
    $found = true;
Sign up to request clarification or add additional context in comments.

2 Comments

OK, Furgas.....thank you. So how would I change it to display results from multiple directories under it's root directory
Great job, Furgas. Thanks so much for your help. Your answer was the one I needed to complete my Plugin for Wordpress. Now to try and submit to Wordpress!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.