Open In App

PHP | FilesystemIterator __construct() Function

Last Updated : 26 Nov, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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
geeks.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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads