0

I'm having an issue with a PHP print statement that repeats the output continuously for about 40 or 50 times, then stops. I thought it was supposed to print only one line. I'm still somewhat new to PHP, so I don't understand what I'm doing wrong. The code in question is located at the bottom of the snippet.

Thanks in advance.....

 <?php
$query = $_POST['query'];
find_files('.');

function find_files($seed) {
    if(! is_dir($seed)) return false;

    $files = array();
    $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)) { 
                        check_files($path); 
                    }
                }
            }
            closedir($dh);
        }
    }
}

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

    if ((isset($str_to_find)) && (empty($str_to_find))) {
        print '<p>Your search produced no results</p>';
    } else {
        if(!($content = file_get_contents($this_file))) { 
            echo("<p>Could not check $this_file</p>\n"); 
        } else { 
            if(stristr($content, $str_to_find)) { 
                echo("<p>$this_file -> contains $str_to_find</p>\n"); 
            }
        }
        unset($content);
    }
}
?>
5
  • Is it 'Your search produced no results' that's repeating? Commented Feb 24, 2012 at 23:33
  • Perhaps you are calling Checkfiles repeatedly (hinted at by spencercw)? ->check_files($path); It's in a while() loop, so it will print every time that sections loops. Commented Feb 24, 2012 at 23:36
  • user978122, thanks for the info. that makes sense.....is there a way to prevent the echo statement from par-taking in this "while" statement? Commented Feb 24, 2012 at 23:40
  • 1
    @RobMyrick Your code is in dire need of proper formatting. Commented Feb 24, 2012 at 23:42
  • @user978122 Just move it outside of the function. See my answer. Commented Feb 24, 2012 at 23:43

3 Answers 3

2

'Your search produced no results' will be printed out once for every file that your loop sees. You should do the check before you call find_files():

if (!isset($str_to_find) || empty($str_to_find)) {
    print '<p>Your search produced no results</p>';
} else {
    find_files('.');
}

You can then remove that bit of code from check_files().

Sign up to request clarification or add additional context in comments.

Comments

0

You have the print statement inside of the check_files() function, which is being called from inside your while... loop. So, yes, it's going to be executed each time that loop executes and the conditions match.

Comments

0

By !== you maybe meant !=?

1 Comment

=== just means 'exactly equals' and doesn't do things like converting strings to ints. It's generally a good idea to always use === and !==.

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.