Wednesday, June 17, 2026
HomeLanguagesPHP | FilesystemIterator __construct() Function

PHP | FilesystemIterator __construct() Function

The FilesystemIterator::__construct() function is an inbuilt function in PHP which is used to construct a new filesystem iterator.

Syntax:

public FilesystemIterator::__construct( string $path, int $flags )

Parameters: This function accepts two parameters as mentioned above and described below:

  • $path: This parameter holds the path of the filesystem item.
  • $flags: This parameter holds the flag which provides the affect and behavior of some methods.

Return Value: This function does not return any value.

Below programs illustrate the FilesystemIterator::__construct() function in PHP:

Program 1:




<?php
  
// Create new file system iterator
$fileItr = new FilesystemIterator(dirname(__FILE__));
  
// Loop runs for each element of filesystem
foreach ($fileItr as $file) {
  
    // Display the filename
    echo $file->getFilename() . "<br>";
}
?>


Output:

applications.html
bitnami.css
dashboard
favicon.ico
neveropen.PNG
gfg.php
img
index.php
Sublime Text Build 3211 x64 Setup.exe
webalizer
xampp

Program 2:




<?php
  
// Create new file system iterator
$fileItr = new FilesystemIterator(dirname(__FILE__));
  
// Loop runs while file iterator is valid
while ($fileItr->valid()) {
  
    // Check for directory files
    if ($fileItr->isDir()) {
  
        // Display the file name
        echo $fileItr->getFilename() . "<br>";
    }
  
    // Move to the next element
    $fileItr->next();
}
  
?>


Output:

dashboard
img
webalizer
xampp

Note: The output of this function depends on the content of server folder.

Reference: https://www.php.net/manual/en/filesystemiterator.construct.php

RELATED ARTICLES

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6898 POSTS0 COMMENTS
Nicole Veronica
12014 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS