php scandir --> 搜索文件/目录

标签 php

我问之前搜索过,没有幸运..

我正在为自己寻找一个可以搜索文件/文件夹的简单脚本。在 php 手册中找到了这个代码片段(我想我需要这个),但它对我不起作用。

“正在寻找一种使用掩码搜索文件/目录的简单方法。这是一个这样的功能。

默认情况下,此函数会将 scandir() 结果保存在内存中,以避免多次扫描同一目录。"

<?php 
function sdir( $path='.', $mask='*', $nocache=0 ){ 
    static $dir = array(); // cache result in memory 
    if ( !isset($dir[$path]) || $nocache) { 
        $dir[$path] = scandir($path); 
    } 
    foreach ($dir[$path] as $i=>$entry) { 
        if ($entry!='.' && $entry!='..' && fnmatch($mask, $entry) ) { 
            $sdir[] = $entry; 
        } 
    } 
    return ($sdir); 
} 
?>

感谢您的帮助,

彼得

最佳答案

$a = new RegexIterator(
    new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator('DIRECTORY HERE')
    ),
    '/REGEX HERE/',
    RegexIterator::MATCH
);

foreach ($a as $v) {
    echo "$v\n"; //$v will be the filename
}

关于php scandir --> 搜索文件/目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2861834/

相关文章:

php - 使用 codeIgniter 查询结果

php - 在 PHP 中获取 mySQL 行的值并回显某些行的多个值

php - MySQL 查询正在使用 MySQL 命令行,但不适用于 MySQLi

php - Symfony 在 Controller 中返回视频

javascript - 将 Javascript 对象传递给 PHP JSON 解码

javascript - 如何使用php获取选择框的数组值作为ajax函数中的数组

javascript - 每个 jquery AJAX 发布一个可变长度的表单

php - 将 .rar 文件解压到 php 中的特定目录

php - 如何用 PHP 解析大的 JSON 文件

php - joomla 中的 jquery-ias 不起作用