读取文件夹中的文件 [英] read files in folder

查看:112
本文介绍了读取文件夹中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,有一个名为video的文件夹.我只想raed这个文件夹的所有文件,我几乎已经完成了,除了.DS_Store之外,它不显示文件夹视频中的任何文件夹 目前我已经申请了条件来隐藏此内容,但是我想要一个完美的解决方案可以提供任何帮助:-我的代码是:----

In my application there is a folder name videos. i want to raed all file only of this folder I have done almostly it does not show any folder in folder videos except .DS_Store currently i have applied if condition to hide this but i want a perfect solution can any one help:- my code is :----

$dir = "../videos/";
$dh  = opendir($dir);

print '<select size="4" name="vidfile">';

while (($file = readdir($dh)) !== false) {

    if (is_file($dir.'/'.$file) && $file != "." && $file != "..") {

        if ($file!=".DS_Store") {

            print "<option>{$file}</option>";
        }
    }          
}

print '</select>'; 

closedir($dh);

推荐答案

快速简便的解决方案

您可以使生活更轻松,并使用 DirectoryIterator 浏览目录.

You could make your life easier and use a DirectoryIterator to go over the directory.

echo '<select name="vids" size="4">';
foreach( new DirectoryIterator('/path/to/videos') as $file) {
    if( $file->isFile() === TRUE && $file->getBasename() !== '.DS_Store') {
        printf("<option>%s</option>\n", htmlentities($file->getBasename()));
    }
}
echo '</select>';


改进:将目录过滤与SelectBox构建脱钩

如果要将过滤逻辑与foreach循环解耦,则可以将FilterIterator子类化,以将该逻辑封装到它的accept()方法中.然后必须将DirectoryIterator包装到FilterIterator中.重点是课程的可重用性:

If you want to decouple the filtering logic from the foreach loop, you can subclass a FilterIterator to capsule that logic into it's accept() method. The DirectoryIterator has to be wrapped into the FilterIterator then. The main point is reusability of course:

class MyFilter extends FilterIterator
{
    public function accept()
    {
        return $this->current()->isFile() === TRUE &&
               $this->current()->getBasename() !== '.DS_Store';
    }
}

$iterator = new MyFilter(new DirectoryIterator('/path/to/videos'));

在过滤的迭代器上使用foreach时,它将自动触发accept().如果accept()返回FALSE,则当前元素将在迭代中被滤除.

When you use foreach on a filtered iterator, it will trigger accept() automatically. If accept() returns FALSE, the current element will be filtered out in the iteration.

然后按以下方式创建SelectBox:

You create the SelectBox like this then:

echo '<select name="vids" size="4">';
foreach( $iterator as $file) {
    printf("<option>%s</option>\n", htmlentities($file->getBasename()));
}
echo '</select>';


替代子类FilterIterator

如果您懒得编写单独的FilterIterator,或者认为对于特定情况不值得,或者已经在某处使用了验证器,并且不想重复其代码,但是仍然想取消筛选和SelectBox的创建,也可以使用此自定义 FilterChainIterator 并为其添加回调:

If you are too lazy to write a separate FilterIterator or think it's just not worth it for a specific case or already have validators somewhere and dont want to duplicate their code, but still want to decouple Filtering and SelectBox creation, you can also use this custom FilterChainIterator and add callbacks to it:

$iterator = new FilterChainIterator(new DirectoryIterator('/path/to/videos'));
$iterator->addCallback(function($file) {
    return $file->isFile() === TRUE &&
           $file->getBasename() !== '.DS_Store';}); 

SelectBox的创建与上面显示的相同.

The SelectBox creation would be the same as shown above.

改进:使SelectBox创建可重复使用

此外,如果要使SelectBox创建可重用,为什么不为其创建一个Helper.下面是一个非常简单的示例,它使用DOM来创建实际的HTML.您传入任何Iterator,它将在您调用render()方法或在字符串上下文中使用它时为您创建HTML:

In addition, if you want to make the SelectBox creation reusable, why not create a Helper for it. Below is a very simple one that uses DOM to create the actual HTML. You pass in any Iterator and it will create the HTML for you when you call it's render() method or use it in a string context:

class SelectBox
{
    protected $iterator;
    public function __construct(Iterator $iterator)
    {
        $this->iterator = $iterator;
    }
    public function render()
    {
        $dom = new DOMDocument;
        $dom->formatOutput = TRUE;
        $dom->loadXml('<select name="vids"/>');
        $dom->documentElement->appendChild(new DOMElement('option', 'Pick One'));
        foreach($this->iterator as $option) {
            $dom->documentElement->appendChild(
                new DOMElement('option', $option));
        }
        return $dom->saveXml($dom->documentElement);
    }
    public function __toString()
    {
        return $this->render();
    }
}

然后从迭代器中打印SelectBox就像

And then printing a SelectBox from an Iterator is as simple as

echo new SelectBox(new MyFilter(new DirectoryIterator('/path/to/videos')));

那么,鉴于所有内容都有迭代器,这将非常灵活.例如

That's pretty flexible then, given that there is Iterators for everything. For instance

echo new SelectBox(new ArrayIterator(array('foo', 'bar', 'baz')));

将提供格式整齐的

<select>
  <option>Pick One</option>
  <option>foo</option>
  <option>bar</option>
  <option>baz</option>
</select>

这篇关于读取文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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