在 PHP 中按文件名搜索目录(包括子目录)中的文件。

huangapple go评论46阅读模式
英文:

search for files in Dir(and subs) by name PHP

问题

我有一个内部网站,用于提供各种培训文档的下载链接。目前,这些文档按部门分类,用户可以导航到相应部门,然后使用PHP来创建指向目录中文件的链接。

我正试图添加一个搜索功能,可以浏览目录(包括子目录),并找到文件名包含特定字符串的文件。

我已经尝试了许多解决方案,这些解决方案是为了解决相同问题的人提出的(它们随处可见,是的,我已经看过并尝试过它们),但似乎无法使其正常工作。 在 PHP 中按文件名搜索目录(包括子目录)中的文件。

具体来说,我找到了一个特定的示例,发布在这里(不是被接受的答案)https://stackoverflow.com/questions/34190464/php-scandir-recursively;

<?php
function rsearch($folder, $pattern) {
    $dir = new RecursiveDirectoryIterator($folder);
    $ite = new RecursiveIteratorIterator($dir);
    $files = new RegexIterator($ite, $pattern, RegexIterator::MATCH);

    foreach ($files as $file) {
        yield $file->getPathName();
    }
}

我认为这个示例应该有效,但我不明白如何访问结果(我认为我是正确调用的)。

请有人能演示如何使用这个函数吗?我会非常感激展示如何调用并访问结果。例如,如果我想找到所有在$folder(包括子文件夹)中包含字符串“apple”的文件,并列出匹配文件的路径/名称。

非常感谢!

英文:

I have an internal website that provides download links to various documents for training purposes. At this point its by department so one just navigates to the department and using php it creates links to the files in a directory.

I am trying to add a search function that will go through a directory(and subs) and find files with files names that contain a specified string.

I have tried many solutions to people asking the same question(they are everywhere, yes I have seen and tried them) on stackoverflow and cant seem to get it to work. 在 PHP 中按文件名搜索目录(包括子目录)中的文件。

Specifically,
I have found a particular example, posted here (not the accepted one)
<https://stackoverflow.com/questions/34190464/php-scandir-recursively>

&lt;?php
 function rsearch($folder, $pattern) {
 $dir = new RecursiveDirectoryIterator($folder);
 $ite = new RecursiveIteratorIterator($dir);
 $files = new RegexIterator($ite, $pattern, RegexIterator::MATCH);


 foreach($files as $file) {
     yield $file-&gt;getPathName();
}}

That I think should work, but I don't understand how to access the results.( I think I am calling correctly)

Can someone please demonstrate how to use this? I would greatly appreciate showing how to call and then access the result. For example if I wanted to find all files in $folder(and sub folders) that contain the string "apple" in the file name and list the path/name for matching files.

Thanks alot!

答案1

得分: 0

已解决!(在互联网的帮助下),并大量使用复制粘贴。

这将查看目录和子目录中的所有文件,并创建HTML链接到文件名包含搜索字符串的任何文件。

<?php
    // 从index.html中的搜索字段获取输入
    $search = $_POST['search'];
    // 存放要搜索的文件的目录
    $path = './files_to_serve';
    // 将搜索字符串转换为小写,文件名将在下面的几行转换为小写
    // 这样搜索结果是不区分大小写的
    $search = strtolower($search);

    // 遍历所有文件
    $iterator = new RecursiveDirectoryIterator($path);   
    foreach(new RecursiveIteratorIterator($iterator) as $file) {
        // 如果它不是目录,并且文件名中包含搜索条件(小写)
        if ($file->isFile() && str_contains(strtolower($file->getFilename()), $search)) {
            // 将所有空格等字符更改为可用于链接的字符串
            $href = rawurlencode($file->getPathName());
            // 打印出文件的链接
            echo "<a href={$href}>{$file->getFileName()}</a><br>";
        }
    }
?>

请注意,这是PHP代码,用于从指定目录及其子目录中查找文件,并创建包含搜索字符串的文件的HTML链接。

英文:

Figured it out! (with much help from the internet), And liberal use of copy paste.

This will look at all files in a directory and sub directories and create html links to any files whose name contains the search string.

    &lt;?php
        // input from search feild in index.html
        $search = $_POST[&#39;search&#39;];
        // directory where files to be searched are located
        $path = &#39;./files_to_serve&#39;;
        // convert search string to lower case, file names will be lower cased a few lines down
        // that way search results are case-insensitive
        $search = strtolower($search);

        // iterate through all the files
        $iterator = new RecursiveDirectoryIterator($path);   
        foreach(new RecursiveIteratorIterator($iterator) as $file) {
            // if its not a directory and the search criteria is in the filename(lowercase)
            if ($file-&gt;isFile() &amp;&amp; str_contains(strtolower($file-&gt;getFilename()), $search)) {
                // change all the spaces etc into a string that will work in a link
                $href = rawurlencode($file-&gt;getPathName());
                // print out the links to the files
                echo &quot;&lt;a href={$href}&gt;{$file-&gt;getFileName()}&lt;/a&gt;&lt;br&gt;&quot;;
            }
        }
    ?&gt;

huangapple
  • 本文由 发表于 2023年2月24日 05:01:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定