简体   繁体   English

PHP 列出目录结构并排除部分目录

[英]PHP List Directory structure and exclude some directories

I have this PHP Code:我有这个 PHP 代码:

$rootpath = '../admin/';
$inner = new RecursiveDirectoryIterator($rootpath);
$fileinfos = new RecursiveIteratorIterator($inner);

foreach ($fileinfos as $pathname => $fileinfo)
{
    $pathname2 = substr($pathname,2);
    $sql = "SELECT * from admin_permissions where page_name = '$pathname2'";
    $rs = mysql_query($sql,$conn);
    if (mysql_num_rows($rs) == 0)
    {
        if (!$fileinfo->isFile()) continue;
        $sql2 = "INSERT into admin_permissions (page_name) values ('$pathname2')";
        $rs2 = mysql_query($sql2,$conn);
        echo "$pathname<br>";
    }
}

That is displaying my directory structure and inserting the directories and file names into a database (removing the first 2 characters .. ).即显示我的目录结构并将目录和文件名插入数据库(删除前 2 个字符.. )。

Since the RecursiveDirectoryIterator iterates through all files in all directories, how can I exclude whole directories, including all files within them?由于RecursiveDirectoryIterator遍历所有目录中的所有文件,如何排除整个目录,包括其中的所有文件?

In essence, my answer is not much different from Thomas ' answer.本质上,我的回答与Thomas的回答没有太大区别。 However, he does not get a few things correct:但是,他没有正确理解以下几点:

  • The semantics correct for the RecursiveCallbackFilterIterator require you to return true to recurse into subdirectories. RecursiveCallbackFilterIterator正确语义要求您返回true以递归到子目录中。
  • He doesn't skip the .他没有跳过. and .. directories inside each sub-directory..每个子目录中的目录
  • His in_array check doesn't quite do what he expects他的in_array检查并不完全符合他的预期

So, I wrote this answer instead.所以,我写了这个答案。 This will work correctly, assuming I understand what you want:这将正常工作,假设我明白你想要什么:

Edit: He has since fixed 2 of those three issues;编辑:他已经修复了这三个问题中的两个; the third may not be an issue because of the way he wrote his conditional check but I am not quite sure.第三个可能不是问题,因为他写条件支票的方式,但我不太确定。

<?php

$directory = '../admin';

// Will exclude everything under these directories
$exclude = array('.git', 'otherDirToExclude');

/**
 * @param SplFileInfo $file
 * @param mixed $key
 * @param RecursiveCallbackFilterIterator $iterator
 * @return bool True if you need to recurse or if the item is acceptable
 */
$filter = function ($file, $key, $iterator) use ($exclude) {
    if ($iterator->hasChildren() && !in_array($file->getFilename(), $exclude)) {
        return true;
    }
    return $file->isFile();
};

$innerIterator = new RecursiveDirectoryIterator(
    $directory,
    RecursiveDirectoryIterator::SKIP_DOTS
);
$iterator = new RecursiveIteratorIterator(
    new RecursiveCallbackFilterIterator($innerIterator, $filter)
);

foreach ($iterator as $pathname => $fileInfo) {
    // do your insertion here
}

I suggest using the RecursiveCallbackFilterIterator.我建议使用 RecursiveCallbackFilterIterator。

$directory = '../admin/';
$filter = array('.git');

$fileinfos = new RecursiveIteratorIterator(
  new RecursiveCallbackFilterIterator(
    new RecursiveDirectoryIterator(
      $directory,
      RecursiveDirectoryIterator::SKIP_DOTS
    ),
    function ($fileInfo, $key, $iterator) use ($filter) {
      return $fileInfo->isFile() || !in_array($fileInfo->getBaseName(), $filter);
    }
  )
);

foreach($fileinfos as $pathname => $fileinfo) {
  //...
}

My suggestion is to try using Symfony's finder library as it makes alot of this much easier and is easily installed via composer here are the docs我的建议是尝试使用 Symfony 的 finder 库,因为它使很多事情变得更容易,并且可以通过 Composer 轻松安装这里是文档

http://symfony.com/doc/current/components/finder.html http://symfony.com/doc/current/components/finder.html

and here is a simple example of something similar to what i think you're asking这是一个简单的例子,类似于我认为你在问什么

<?php
use Symfony\Component\Finder\Finder;

/**
 * @author Clark Tomlinson  <fallen013@gmail.com>
 * @since 12/6/13, 12:52 PM
 * @link http://www.clarkt.com
 * @copyright Clark Tomlinson © 2013
 *
 */

require_once('vendor/autoload.php');

$finder = new Finder();
$directories = $finder->directories()
                      ->in(__DIR__)
                      ->ignoreDotFiles(true)
                      ->exclude(array('one', 'two', 'three', 'four'))
                      ->depth(0);

foreach ($directories as $dir) {
    echo '<pre>';
    print_r($dir->getRealPath());
    echo '</pre>';
}

That example will return all directories without transversing into them to change that change or remove the depth.该示例将返回所有目录,而无需遍历它们以更改该更改或删除深度。

To get all files in that directory do something similar to this要获取该目录中的所有文件,请执行与此类似的操作

<?php
use Symfony\Component\Finder\Finder;

/**
 * @author Clark Tomlinson  <fallen013@gmail.com>
 * @since 12/6/13, 12:52 PM
 * @link http://www.clarkt.com
 * @copyright Clark Tomlinson © 2013
 *
 */

require_once('vendor/autoload.php');

$finder = new Finder();
$directories = $finder->directories()
                      ->in(__DIR__)
                      ->ignoreDotFiles(true)
                      ->exclude(array('one', 'two', 'three', 'four'))
                      ->depth(0);

foreach ($directories as $dir) {
    $files = $finder->files()
                    ->ignoreDotFiles(true)
                    ->in($dir->getRealPath());

    foreach ($files as $file) {
        echo '<pre>';
        print_r($file);
        echo '</pre>';
    }
}

You can use RecursiveFilterIterator to filter dirs and in fooreach loop you have only accepted dirs.您可以使用 RecursiveFilterIterator 来过滤目录,并且在 fooreach 循环中您只接受目录。

class MyDirFilter extends RecursiveFilterIterator {
    public function accept() {

        $excludePath = array('exclude_dir1', 'exclude_dir2');
        foreach($excludePath as $exPath){
            if(strpos($this->current()->getPath(), $exPath) !== false){
                return false;
            }
        }
        return true;

    }
}

$rootpath = '../admin/';
$dirIterator = new RecursiveDirectoryIterator($rootpath);
$filter   = new MyDirFilter($dirIterator);
$fileinfos   = new RecursiveIteratorIterator($filter);

foreach($fileinfos as $pathname => $fileinfo)
{
    // only accepted dirs in loop
}

I would keep it simpler, with some basic php functions, easy to read (no thirty part, no delegations to other objects), and this few lines.我会让它更简单,有一些基本的 php 函数,易于阅读(没有三十部分,没有对其他对象的委托),还有这几行。

First, you create a function,that receives the dir you want to explore, and an optional array or excluded filenames首先,您创建一个函数,它接收您要探索的目录,以及一个可选的数组或排除的文件名

function getFiles($dir , $exclude=array()){  
    $acceptedfiles=array();  
    $handle=opendir($dir );
    //reads the filenames, one by one   
    while ($file = readdir($handle)) {
        if ($file!="." && $file!=".." && is_file($file) && !in_array($file, $exclude)) 
        $acceptedfiles[]=$file;
    }
    closedir($handle); 
    return $acceptedfiles;
}

Now, you just have to call the function现在,你只需要调用函数

$files= getFiles($rootpath) ;

or if you want to exclude some files if they exist, you can call或者如果你想排除一些存在的文件,你可以调用

$files= getFiles($rootpath, array(".htaccess","otherfile", "etc")) ;

I would do it like this:我会这样做:

$excludes = array(
    'files' => array('file1.ext','file2.ext','fileN.ext'),
    'dirs' => array('dir1','dir2','dirN')
);

foreach ($directories as $current_dir) {
    if (!in_array($current_dir,$excludes['dirs'])) {
       // directory method
       foreach ($files as $current_file) {
            if (!in_array($current_file,$excludes['files'])) {
              // files method
            }
        }
    }
}

If I correct guess you then you just want add to base only path for files, not for directory.如果我猜对了你,那么你只想添加到文件的基本路径,而不是目录。

Just use for check function is_file in your foreach只需在 foreach 中用于检查函数is_file

For example例如

foreach ($fileinfos as $pathname => $fileinfo)
{

    if (!is_file($pathname)) 
    {
         continue;// next step
    }
    $pathname2 = substr($pathname,2);
    ...
}

I feel that some of the other answers are good, but more verbose than they need to be.我觉得其他一些答案很好,但比他们需要的要冗长。 Here is a some simple code.这是一些简单的代码。 My example only filters the .git directory, but you can easily expand it to other items:我的示例仅过滤.git目录,但您可以轻松将其扩展到其他项目:

<?php

$f_filter = fn ($o_file) => $o_file->getFilename() == '.git' ? false : true;
$o_dir = new RecursiveDirectoryIterator('.');
$o_filter = new RecursiveCallbackFilterIterator($o_dir, $f_filter);
$o_iter = new RecursiveIteratorIterator($o_filter);

foreach ($o_iter as $o_file) {
   echo $o_file->getPathname(), "\n";
}

https://php.net/class.recursivecallbackfilteriterator https://php.net/class.recursivecallbackfilteriterator

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM