« RecursiveDirectoryIterator
RecursiveDirectoryIterator::getChildren »

RecursiveDirectoryIterator::__construct

(PHP 5 >= 5.1.2, PHP 7, PHP 8)

RecursiveDirectoryIterator::__construct — RecursiveDirectoryIterator を作成する

説明

public function RecursiveDirectoryIterator::__construct(string $directory, int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO)

指定したパス directory の RecursiveDirectoryIterator() を作成します。

パラメータ

directory

反復処理の対象となるディレクトリのパス。

flags

フラグを指定して、いくつかのメソッドの振る舞いを変更することができます。 フラグの一覧は FilesystemIterator の定義済み定数 にあります。フラグは、あとから FilesystemIterator::setFlags() で設定することもできます。

エラー / 例外

directory が存在しない場合に、 UnexpectedValueException をスローします。

directory が空文字列の場合に、 ValueError をスローします。

変更履歴

バージョン 説明
8.0.0 directory が空文字列の場合に、 ValueError をスローするようになりました。 これより前のバージョンでは、 RuntimeException をスローしていました。

例

例1 RecursiveDirectoryIterator の例

<?php

$directory = '/tmp';

$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));

$it->rewind();
while($it->valid()) {

    if (!$it->isDot()) {
        echo 'SubPathName: ' . $it->getSubPathName() . "\n";
        echo 'SubPath:     ' . $it->getSubPath() . "\n";
        echo 'Key:         ' . $it->key() . "\n\n";
    }

    $it->next();
}

?>

上の例の出力は、 たとえば以下のようになります。

SubPathName: fruit/apple.xml
SubPath:     fruit
Key:         /tmp/fruit/apple.xml

SubPathName: stuff.xml
SubPath:     
Key:         /tmp/stuff.xml

SubPathName: veggies/carrot.xml
SubPath:     veggies
Key:         /tmp/veggies/carrot.xml

参考

関連キーワード:
RecursiveDirectoryIterator   directory   FilesystemIterator   construct   SubPathName   SubPath   フラグ   RecursiveIteratorIterator   fruit   veggies   flags   定数   ValueError   apple   stuff   carrot   getChildren   反復   setFlags   UnexpectedValueException   履歴   スロー   RuntimeException   rewind   while
関連する関数・ページ:
recursivedirectoryiterator.getsubpath   recursivedirectoryiterator.getsubpathname   recursivedirectoryiterator.getchildren   recursivedirectoryiterator.next   recursivedirectoryiterator.key   recursivedirectoryiterator.rewind