簡體   English   中英

腳本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/");

這是一個無需PHP即可執行的bash腳本。 將其上傳到您的服務器,然后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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM