编写搜索文件的php脚本


Script php that search files

我正在使用Drupal7进行一个项目,该项目将PDF文件上传到一个目录中,我想知道是否有一个PHP脚本可以让我在上传的文件中搜索。例如,如果我在搜索框中写"JAVA",它将返回所有包含"JAVA"的文件作为内容的一部分。

function searchFiles($needle, $haystack){
    $found = array();
    $FILES = scandir($haystack);
    foreach($FILES as $FILE){
        if("." === substr($FILE, 0, 1)) continue;
        try{
            $content = file_get_contents($haystack."/".$FILE);
            if(strpos($content, $needle) !== false) array_push($found, $FILE);
        }catch(Exception $e){}
    }
    return $found;
}
$files = searchFiles("some text", "/var/www/html/");

这里有一个bash脚本,它将在没有PHP的情况下完成。将它上传到服务器,然后chmod +x,这样你就可以运行它。然后你可以用exec从php调用脚本。

#!/bin/bash
# Recursively searches the all text files in the current working directoy
# for a given pattern. Search is case-insensitive and regex may be used in the patteren.
# Matches are highlighted.
# Usage: $ lazygrep "somepattern"
PATTERN=$1
DIRSS=$(pwd)
clear
printf "'n'nSEARCHING FOR $PATTERN IN $DIRSS'n'n"
grep -inIEr --color=ALWAYS "$PATTERN" $DIRSS
printf "'n'n"