【问题标题】:Php extend RecursiveFilterIterator, show and ignore filesphp 扩展 RecursiveFilterIterator,显示和忽略文件
【发布时间】:2016-05-23 03:49:32
【问题描述】:

我正在使用类似的东西:

class MyRecursiveFilterIterator extends RecursiveFilterIterator {

    public static $FILTERS = array(
        '.htaccess',
        '.html',
    );

    public function accept() {
        return !in_array(
            $this->current()->getFilename(),
            self::$FILTERS,
            true
        );
    }

}

$iterator = new RecursiveDirectoryIterator($dir);
$iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
$filter = new MyRecursiveFilterIterator($iterator);
$all_files  = new RecursiveIteratorIterator($filter,RecursiveIteratorIterator::SELF_FIRST);

列出指定 $dir 路径中的所有文件和文件夹。

MyRecursiveFilterIterator 扩展了默认的 RecursiveFilterIterator 并排除了某些文件扩展名,如 .htaccess 和 html。现在我想添加仅显示某些文件类型(如 .jpg、.png 以及保留排除的文件类型)的功能。

【问题讨论】:

    标签: php iterator


    【解决方案1】:

    嗨,

    我只指您问题的第一部分,即:“...现在我想添加仅显示某些文件类型的功能,例如 .jpg、.png ...”

    因此,如果您想过滤某些文件类型,您可以使用 RegexIterator,这里原则上显示的是 .png 文件。

    <?php
            $Directory = new RecursiveDirectoryIterator("../");
            $Iterator = new RecursiveIteratorIterator($Directory);
            // here the .png Match is set
            $Regex = new RegexIterator($Iterator, '/^.+\.png$/i', RecursiveRegexIterator::GET_MATCH);
            // the following part is only for the output of the resulting array $Regex in html
            $output = "<table width='100%' align='center'><table width='50%' align='center'>";
             foreach($Regex as $key => $var) {
                        $output .= '<tr>';
                        foreach($var as $col => $val) {
                            $output .= "<td style='font-size:14px;font-weight:bold;'>" . $col . '</td>';
                        }
                        $output .= '</tr>';
                        foreach($var as $col => $val) {
                            $output .= '<td>' . $val . '</td>';
                        }
                        $output .= '</tr>';
                    }
             $output .= '</table></table>';
             echo $output;
    ?>
            <head>
                <style type="text/css">
                    table , tr , td {
                        font: arial;
                        font-size: 13px;
                        font-style: normal;
                        font-weight: normal;
                        border: 1px solid blue;
                        border-collapse: collapse;
                    }
                </style>
            </head>
    

    ... 等等。自然这只是一个粗略的解决方案,只是说明了原理。

    最好的问候 Axel Arnold Bangert - 黑措根拉特 2016

    【讨论】:

      猜你喜欢
      • 2018-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-14
      • 1970-01-01
      • 1970-01-01
      • 2018-08-12
      相关资源
      最近更新 更多