downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

DirectoryIterator::__construct> <CachingIterator::valid
Last updated: Fri, 14 Aug 2009

view this page in

La classe DirectoryIterator

Introduction

La classe DirectoryIterator fournit une interface simple pour lire le contenu d'un système de fichiers.

Synopsis de la classe

DirectoryIterator
DirectoryIterator extends SplFileInfo implements Iterator , Traversable , SeekableIterator {
/* Méthodes */
__construct ( string $path )
public DirectoryIterator current ( void )
public int getATime ( void )
public string getBasename ([ string $suffix ] )
public int getCTime ( void )
public string getFilename ( void )
public int getGroup ( void )
public int getInode ( void )
public int getMTime ( void )
public int getOwner ( void )
public string getPath ( void )
public string getPathname ( void )
public int getPerms ( void )
public int getSize ( void )
public string getType ( void )
public bool isDir ( void )
public bool isDot ( void )
public bool isExecutable ( void )
public bool isFile ( void )
public bool isLink ( void )
public bool isReadable ( void )
public bool isWritable ( void )
publicstring key ( void )
publicvoid next ( void )
public void rewind ( void )
public void seek ( int $position )
public string __toString ( void )
public bool valid ( void )
}

Sommaire



DirectoryIterator::__construct> <CachingIterator::valid
Last updated: Fri, 14 Aug 2009
 
add a note add a note User Contributed Notes
DirectoryIterator
krystianmularczyk at gmail dot com
25-Jan-2009 11:31
Shows us all files and catalogues in directory except "." and "..".

<?php

foreach (new DirectoryIterator('../moodle') as $fileInfo) {
    if(
$fileInfo->isDot()) continue;
    echo
$fileInfo->getFilename() . "<br>\n";
}

?>
David Lanstein
21-Jan-2009 08:50
DirectoryIterator::getBasename() has been also been available since 5.2.2, according to the changelog (not documented yet).  It takes a parameter $suffix, and is useful if, for instance, you use a naming convention for your files (e.g. ClassName.php). 

The following code uses this to add recursively All*Tests.php in any subdirectory off of tests/, basically, suites of suites.

<?php
// PHPUnit boilerplate code goes here

class AllTests {
    public static function
main() {
       
$parameters = array('verbose' => true);
       
PHPUnit_TextUI_TestRunner::run(self::suite(), $parameters);
    }

    public static function
suite() {
       
$suite = new PHPUnit_Framework_TestSuite('AllMyTests'); // this must be something different than the class name, per PHPUnit
       
$it = new AllTestsFilterIterator(
                  new
RecursiveIteratorIterator(
                      new
RecursiveDirectoryIterator(dirname(__FILE__) . '/tests')));

        for (
$it->rewind(); $it->valid(); $it->next()) {
            require_once(
$it->current());
           
$className = $it->current()->getBasename('.php');
           
$suite->addTest($className::suite());
        }

        return
$suite;
    }
}
?>

Also, the AllTestsFilterIterator above extends FilterIterator, and contains one method, accept():

<?php
class AllTestsFilterIterator extends FilterIterator {
    public function
accept() {
        if (
preg_match('/All.*Tests\.php/', $this->current())) {
            return
true;
        } else {
            return
false;
        }
    }
}
?>
Mark van Straten
09-Jul-2008 02:56
Implements Iterator so you can foreach() over the content of the given directory

DirectoryIterator::__construct> <CachingIterator::valid
Last updated: Fri, 14 Aug 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites