PHP | DirectoryIterator __construct() Function
The DirectoryIterator::__construct() function is an inbuilt function in PHP which is used to construct a new directory iterator from a path.
Syntax:
public DirectoryIterator::__construct( string $path )
Parameters: This function accepts single parameter $path which holds the path of the directory to traverse.
Return Value: This function does not return any value.
Below programs illustrate the DirectoryIterator::__construct() function in PHP:
Program 1:
php
<?php
$directory = new DirectoryIterator(dirname( __FILE__ ));
foreach ( $directory as $dir ) {
if ( $dir ->valid()) {
echo $dir ->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
<?php
$directory = new DirectoryIterator(dirname( __FILE__ ));
while ( $directory ->valid()) {
if ( $directory ->isDir()) {
echo $directory ->getFilename() . "<br>";
}
$directory ->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/directoryiterator.construct.php
Last Updated :
27 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...