php scandir->搜索文件/目录 [英] php scandir --> search for files/directories

查看:86
本文介绍了php scandir->搜索文件/目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在问之前就进行了搜索,但并不幸运.

I searched before I ask, without lucky..

我正在寻找适合自己的简单脚本,可以搜索文件/文件夹.在php手册中找到了此代码段(我想我需要这个代码段),但是对我来说不起作用.

I looking for a simple script for myself, which I can search for files/folders. Found this code snippet in the php manual (I think I need this), but it is not work for me.

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

"Was looking for a simple way to search for a file/directory using a mask. Here is such a function.

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

By default, this function will keep in memory the scandir() result, to avoid scaning multiple time for the same directory."

<?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-&gt;搜索文件/目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆