PHP函数不会重复

时间:2012-02-26 20:56:08

标签: php

我试图找出为什么下面的整个代码在运行所有函数后只产生一个结果,而不是产生多个结果。

代码应该从基于文本的搜索中获取单个结果,然后检查用户服务器上的每个文件并找到该字符串,然后输出包含该搜索字符串的每个文件。

代码只生成一个结果,而不是输出包含输入字符串的所有文件。

我曾经一次使用此功能,但代码是在本网站上的先前问题中修改过来的,现在效果不正确。

<?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; 
        } 
    } 
} 

?>

1 个答案:

答案 0 :(得分:1)

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

此if子句只允许找到一个文件。替换为

if (check_files($path))
    $found = true;