如何使用RecursiveDirectoryIterator排除目录 [英] How can I exclude directories using RecursiveDirectoryIterator

查看:141
本文介绍了如何使用RecursiveDirectoryIterator排除目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下功能.它遍历目录并递归搜索它们以获取随机图像文件,然后将其附加到帖子.我想做的是从搜索中排除一些文件.

I have the function below. Which goes through directories and recursively searches through them to grab a random image file and then attaches that to a post. What I want to do is exclude some files from the search.

我有一个用逗号分隔的列表,将其分解成一个数组,我尝试使用过滤器,但无法正常工作.

I have a comma separated list which I explode into an array, I tried using a filter but couldn't get this to work.

不带过滤器的当前功能是

Current function without filter is

function swmc_get_imgs($start_dir, $ext, $exclude=array()){
$dir   = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($start_dir));
$files = array();

// Force array of extensions and make them all lower-case
if ( ! is_array($ext))
{
    $ext = (array) $ext;
}

$ext = array_unique(array_map('strtolower', $ext));

foreach($dir as $file)
{
    // Skip anything that isn't a file
    if ( ! $file->isFile())
        continue;

    // If the file has one of our desired extensions, add it to files array
    if (in_array(strtolower(pathinfo($file->getFilename(), PATHINFO_EXTENSION)), $ext)) {
        $files[] = $file->getPathname();
    }
}

return $files;
}

因此上述方法有效,但仍然可能相当昂贵,尤其是对于许多目录而言,因此我想排除存储在逗号列表中的目录列表.

So the above works but can be fairly expensive still especially with a lot of directories, as such I want to exclude a list of directories stored in a comma list.

我尝试了以下

class SwmcOnlyFilter extends RecursiveFilterIterator {
public function accept() {
    // Accept the current item if we can recurse into it
    // or it is a value starting with "test"
    return $this->hasChildren() || !in_array($this->current(),     explode(",",get_option('swmc_image_excl')));
}
}

然后将swmc_get_imgs函数的第一部分更改为

And then changing the first part of the swmc_get_imgs function to

$dirIterator = new RecursiveDirectoryIterator($start_dir);
$filter   = new SwmcOnlyFilter($dirIterator);
$dir   = new RecursiveIteratorIterator($filter);

但是,过滤器不会跳过该目录,而是会进入该目录.

However the filter doesn't jump over that directory but instead goes into it.

目录看起来像

/uploads/2009/1/2011_pic.jpg
/uploads/2011/1/john_smith.jpg

以此类推.

因此,我可能想排除2011作为目录,但不排除标题为2011且位于2009年的图像.

So I may want to exclude 2011 as a directory but not exclude the image that lives in 2009 with 2011 in its title.

澄清:

我可以通过在foreach循环中跳过它们来手动过滤掉它们,但是这仍然会检查它们并浪费内存和时间.如果可能的话,我宁愿跳过这些.

I could filter out these manually by skipping them in the foreach loop, however this still checks them and wastes memory and time. I would prefer to skip these at the time of the grab if possible.

推荐答案

使用以下内容将其弄清楚

figured it out using the following

function swmc_iterate_imgs($start_dir)  {
$directory = $start_dir; 
$excludedDirs = explode(",",get_option('swmc_image_excl')); // array of subdirectory paths, relative to $directory, to exclude from search 
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)); 
$fileArr = array();  // numerically indexed array with your files 
$x = -1; 
while ($it->valid()) 
{ 
    if (!$it->isDot() && !in_array($it->getSubPath(), $excludedDirs) && preg_match('/(\.(jpg|jpeg|gif|png))$/i', $it->key()) == 1) 
    { 
        $fileArr[] = $it->key(); 
    } 
    $it->next(); 
} 

return $fileArr;
}  

这篇关于如何使用RecursiveDirectoryIterator排除目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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