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

时间:2022-09-01 23:08:40

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.

如果文件位于我的域名根目录内,该脚本工作正常...现在我正在尝试找到一种方法使它也扫描子文件夹和子子文件夹,但我被困在这里。

3 个解决方案

#1


46  

There are 2 ways.

有两种方法。

Use glob to 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;
}
?>

RecursiveDirectoryIterator comes with PHP5 while glob is from PHP4. Both can do the job, it's up to you.

RecursiveDirectoryIterator附带PHP5,而glob来自PHP4。两者都能胜任,这取决于你。

#2


2  

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://*.com/a/1860417/219112

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

#3


0  

I think this is more semantic

我认为这更具语义性

$ReDirectory = new \RecursiveDirectoryIterator('path/to/search');
$ReIterator = new \RecursiveIteratorIterator($ReDirectory );

while($ReIterator->valid()) {

    if ($ReIterator->getFilename() == $search && ! $ReIterator ->isDot()) {
        echo '<a href="http://www.example.com/' . $ReIterator->getFilename() . '">' . $search . '</a>';
    }

    $Rii->next();
}

#1


46  

There are 2 ways.

有两种方法。

Use glob to 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;
}
?>

RecursiveDirectoryIterator comes with PHP5 while glob is from PHP4. Both can do the job, it's up to you.

RecursiveDirectoryIterator附带PHP5,而glob来自PHP4。两者都能胜任,这取决于你。

#2


2  

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://*.com/a/1860417/219112

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

#3


0  

I think this is more semantic

我认为这更具语义性

$ReDirectory = new \RecursiveDirectoryIterator('path/to/search');
$ReIterator = new \RecursiveIteratorIterator($ReDirectory );

while($ReIterator->valid()) {

    if ($ReIterator->getFilename() == $search && ! $ReIterator ->isDot()) {
        echo '<a href="http://www.example.com/' . $ReIterator->getFilename() . '">' . $search . '</a>';
    }

    $Rii->next();
}