【问题标题】:Script php that search files搜索文件的脚本 php
【发布时间】:2016-04-05 18:02:52
【问题描述】:

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

【问题讨论】:

    标签: php file search drupal-7


    【解决方案1】:
    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"
    

    【讨论】:

    • 感谢您的评论,但我收到了所有这些警告:警告:is_dir():无法找到“公共”包装器 - 您在配置 PHP 时是否忘记启用它?在 file_prepare_directory()(.....) 警告:is_dir(): Unable to find the wrapper "public" - 你在配置 PHP 时忘记启用它了吗?在 file_prepare_directory()(.....) 警告:is_dir(): Unable to find the wrapper "public" - 你在配置 PHP 时忘记启用它了吗?在 file_prepare_directory()(.....)
    • 警告:is_dir(): 无法找到包装器“public” - 您在配置 PHP 时是否忘记启用它?在 file_prepare_directory()(.....) 警告:is_dir(): Unable to find the wrapper "public" - 你在配置 PHP 时忘记启用它了吗?在 file_prepare_directory()(.....) 警告:is_dir(): Unable to find the wrapper "public" - 你在配置 PHP 时忘记启用它了吗?在 file_prepare_directory()(.....) 我认为是因为这个: $filepath="public://"; $url=file_create_url($filepath); $files = searchFiles("一些文本", $url);
    猜你喜欢
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    • 2015-02-05
    • 2023-03-13
    • 2010-10-31
    • 2012-06-09
    • 1970-01-01
    相关资源
    最近更新 更多