Friday, November 14, 2025
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
32399 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6765 POSTS0 COMMENTS
Nicole Veronica
11917 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11984 POSTS0 COMMENTS
Shaida Kate Naidoo
6889 POSTS0 COMMENTS
Ted Musemwa
7142 POSTS0 COMMENTS
Thapelo Manthata
6837 POSTS0 COMMENTS
Umr Jansen
6840 POSTS0 COMMENTS