duanhuan6857 2012-02-26 20:56
浏览 48
已采纳

PHP函数不会重复

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>
"); 
        return false; 
    } 
    else 
    { 
        if (stristr($content, $str_to_find)) 
        { 
            echo("<p style=\"color:#575757; font-family:arial\">$this_file -> contains $str_to_find</p>
"); 
            return true; 
        } 
    } 
} 

?>
  • 写回答

1条回答 默认 最新

  • douzhongju8780 2012-02-26 21:08
    关注
    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)