php glob - 在子文件夹中扫描文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17160696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:24:40  来源:igfitidea点击:

php glob - scan in subfolders for a file

phpfilesearchglob

提问by Winston Smith

I have a server with a lot of files inside various folders, sub-folders, and sub-sub-folders.

我有一台服务器,在各种文件夹、子文件夹和子子文件夹中都有很多文件。

I'm trying to make a search.php page that would be used to search the whole server for a specific file. If the file is found, then return the location path to display a download link.

我正在尝试制作一个 search.php 页面,用于在整个服务器中搜索特定文件。如果找到该文件,则返回位置路径以显示下载链接。

Here's what i have so far:

这是我到目前为止所拥有的:

$root = $_SERVER['DOCUMENT_ROOT'];
$search = "test.zip";
$found_files = glob("$root/*/test.zip");
$downloadlink = str_replace("$root/", "", $found_files[0]);
if (!empty($downloadlink)) {
    echo "<a href=\"http://www.example.com/$downloadlink\">$search</a>";
} 

The script is working perfectly if the file is inside the root of my domain name... Now i'm trying to find a way to make it also scan sub-folders and sub-sub-folders but i'm stuck here.

如果文件在我的域名的根目录中,该脚本运行良好......现在我正在尝试找到一种方法让它也扫描子文件夹和子子文件夹,但我被困在这里。

回答by Tony Chen

There are 2 ways.

有2种方式。

Use globto do recursive search:

用于glob进行递归搜索:

<?php

// Does not support flag GLOB_BRACE
function rglob($pattern, $flags = 0) {
    $files = glob($pattern, $flags); 
    foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
        $files = array_merge($files, rglob($dir.'/'.basename($pattern), $flags));
    }
    return $files;
}

?>

Use RecursiveDirectoryIterator

RecursiveDirectoryIterator

<?php
function rsearch($folder, $pattern) {
    $dir = new RecursiveDirectoryIterator($folder);
    $ite = new RecursiveIteratorIterator($dir);
    $files = new RegexIterator($ite, $pattern, RegexIterator::GET_MATCH);
    $fileList = array();
    foreach($files as $file) {
        $fileList = array_merge($fileList, $file);
    }
    return $fileList;
}
?>

RecursiveDirectoryIteratorcomes with PHP5 while globis from PHP4. Both can do the job, it's up to you.

RecursiveDirectoryIterator来自 PHP5 而glob来自 PHP4。两者都可以胜任,这取决于您。

回答by Quasimodo's clone

I want to provide another simple alternative for cases where you can predict a max depth. You can use a pattern with braces listing all possible subfolder depths.

我想为您可以预测最大深度的情况提供另一种简单的替代方法。您可以使用带括号的模式列出所有可能的子文件夹深度。

This example allows 0-3 arbitrary subfolders:

此示例允许 0-3 个任意子文件夹:

glob("$root/{,*/,*/*/,*/*/*/}test_*.zip", GLOB_BRACE);

Of course the braced pattern could be procedurally generated.

当然,可以通过程序生成支撑模式。

回答by Sadee

This returns fullpath to the file

这将返回文件的完整路径

function rsearch($folder, $pattern) {
    $iti = new RecursiveDirectoryIterator($folder);
    foreach(new RecursiveIteratorIterator($iti) as $file){
         if(strpos($file , $pattern) !== false){
            return $file;
         }
    }
    return false;
}

call the function:

调用函数:

$filepath = rsearch('/home/directory/thisdir/', "/findthisfile.jpg");

And this is returns like:

这是返回如下:

/home/directory/thisdir/subdir/findthisfile.jpg

/home/directory/thisdir/subdir/findthisfile.jpg

You can improve this function to find several files like all jpeg file:

您可以改进此功能以查找多个文件,例如所有 jpeg 文件:

function rsearch($folder, $pattern_array) {
    $return = array();
    $iti = new RecursiveDirectoryIterator($folder);
    foreach(new RecursiveIteratorIterator($iti) as $file){
        if (in_array(strtolower(array_pop(explode('.', $file))), $pattern_array)){
            $return[] = $file;
        }
    }
    return $return;
}

This can call as:

这可以称为:

$filepaths = rsearch('/home/directory/thisdir/', array('jpeg', 'jpg') );

Ref: https://stackoverflow.com/a/1860417/219112

参考:https: //stackoverflow.com/a/1860417/219112

回答by metzelder

As a full solution for your problem (this was also my problem):

作为您问题的完整解决方案(这也是我的问题):

<?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();
    }
}

Will get you the full path of the items that you wish to find.

将为您提供要查找的项目的完整路径。

Edit: Thanks to Rousseau Alexandrefor pointing out , $pattern must be regular expression.

编辑:感谢Rousseau Alexandre指出,$pattern 必须是正则表达式。