略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: DirectoryIterator

2024-11-13

The DirectoryIterator class

(PHP 5, PHP 7, PHP 8)

简介

The DirectoryIterator class provides a simple interface for viewing the contents of filesystem directories.

类摘要

class DirectoryIterator extends SplFileInfo implements SeekableIterator {
/* 方法 */
public __construct(string $directory)
public current(): mixed
public getATime(): int
public getBasename(string $suffix = ""): string
public getCTime(): int
public getExtension(): string
public getFilename(): string
public getGroup(): int
public getInode(): int
public getMTime(): int
public getOwner(): int
public getPath(): string
public getPathname(): string
public getPerms(): int
public getSize(): int
public getType(): string
public isDir(): bool
public isDot(): bool
public isExecutable(): bool
public isFile(): bool
public isLink(): bool
public isReadable(): bool
public isWritable(): bool
public key(): mixed
public next(): void
public rewind(): void
public seek(int $offset): void
public __toString(): string
public valid(): bool
/* 继承的方法 */
public SplFileInfo::getATime(): int|false
public SplFileInfo::getBasename(string $suffix = ""): string
public SplFileInfo::getCTime(): int
public SplFileInfo::getExtension(): string
public SplFileInfo::getFileInfo(?string $class = null): SplFileInfo
public SplFileInfo::getFilename(): string
public SplFileInfo::getGroup(): int|false
public SplFileInfo::getInode(): int|false
public SplFileInfo::getLinkTarget(): string|false
public SplFileInfo::getMTime(): int|false
public SplFileInfo::getOwner(): int|false
public SplFileInfo::getPath(): string
public SplFileInfo::getPathInfo(?string $class = null): ?SplFileInfo
public SplFileInfo::getPathname(): string
public SplFileInfo::getPerms(): int|false
public SplFileInfo::getRealPath(): string|false
public SplFileInfo::getSize(): int|false
public SplFileInfo::getType(): string|false
public SplFileInfo::isDir(): bool
public SplFileInfo::isFile(): bool
public SplFileInfo::isLink(): bool
public SplFileInfo::openFile(string $mode = "r", bool $useIncludePath = false, ?resource $context = null): SplFileObject
public SplFileInfo::setFileClass(string $class = SplFileObject::class): void
public SplFileInfo::setInfoClass(string $class = SplFileInfo::class): void
public SplFileInfo::__toString(): string
}

目录

add a noteadd a note

User Contributed Notes 8 notes

up
55
krystianmularczyk at gmail dot com
13 years ago
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";
}

?>
up
2
alvaro at demogracia dot com
4 years ago
DirectoryIterator is just an lightweight SplFileInfo iterator and its methods operate on whatever item the internal cursor points to. In other words:

<?php
$iterator
= new DirectoryIterator('C:\\');
echo
$iterator->getPathname();
?>

... will NOT print "C:\" but the path of the first file or subdirectory retrieved, e.g. "C:\$Recycle.Bin".
up
9
rogier at dsone dot nl
9 years ago
Beware of the behavior when using FilesystemIterator::UNIX_PATHS, it's not applied as you might expect.

I guess this flag is added especially for use on windows.
However, the path you construct the RecursiveDirectoryIterator or FilesystemIterator with will not be available as a unix path.
I can't say this is a bug, since most methods are just purely inherited from DirectoryIterator.

In my test, I'd expected a complete unix path. Unfortunately... not quite as expected:

<?php
        
// say $folder = C:\projects\lang

       
$flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS | FilesystemIterator::UNIX_PATHS;
       
$d_iterator = new RecursiveDirectoryIterator($folder, $flags);

        echo
$d_iterator->getPath();

?>

expected result: /projects/lang (or C:/projects/lang)
actual result: C:\projects\lang
up
4
David Lanstein
13 years ago
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;
        }
    }
}
?>
up
-5
Mark van Straten
13 years ago
Implements Iterator so you can foreach() over the content of the given directory
up
-6
Kunal Bhatia->kmasterzone@gmail dot com
11 years ago
Deleting all files in a directory except the one which is last modified.
<?php
    $directory
= dirname(__FILE__)."/demo";
   
$filenames = array();
   
$iterator = new DirectoryIterator($directory);
    foreach (
$iterator as $fileinfo) {
        if (
$fileinfo->isFile()) {
           
$filenames[$fileinfo->getMTime()] = $fileinfo->getFilename();
        }
    }
   
ksort($filenames);
   
print_r($filenames);
    echo
"\n";
   
$i=0;
    if(
sizeof($filenames)>1){
        foreach (
$filenames as $file){
            if(
$i>0){
                echo
$file."\n";
               
unlink($directory."/".$file);
            }
           
$i++;
        }
    }
?>
up
-6
abdel at elghafoud dot com
8 years ago
Shows us recursively all files and catalogues in directory except "." and "..".

<?php

/**
* @param string $directory
* @param array $files
* @return array
*/
public function recursiveDirectoryIterator ($directory = null, $files = array()) {
   
$iterator = new \DirectoryIterator ( $directory );

    foreach (
$iterator as $info ) {
        if (
$info->isFile ()) {
           
$files [$info->__toString ()] = $info;
        } elseif (!
$info->isDot ()) {
           
$list = array($info->__toString () => $this->recursiveDirectoryIterator(
                       
$directory.DIRECTORY_SEPARATOR.$info->__toString ()
            ));
            if(!empty(
$files))
               
$files = array_merge_recursive($files, $filest);
            else {
               
$files = $list;
            }
        }
    }
    return
$files;
}

?>
up
-9
incredimike
4 years ago
For some reason, the `isDot()` method doesn't return positive for the `.DS_Store` file generated by Mac OS's Finder. In practice, I've had to write code to explicitly check for this file:

<?php

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

?>

官方地址:https://www.php.net/manual/en/class.directoryiterator.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3