PHP | DirectoryIterator __toString() Function
The DirectoryIterator::__toString() function is an inbuilt function in PHP which is used to return the file name of the current DirectoryIterator item.
Syntax:
string DirectoryIterator::__toString( void )
Parameters: This function does not accept any parameters.
Return Value: This function returns the file name of the current DirectoryIterator item.
Below programs illustrate the DirectoryIterator::__toString() function in PHP:
Program 1:
php
<?php
$directory = new DirectoryIterator(dirname( __FILE__ ));
while ( $directory ->valid()) {
echo $directory ->__toString() . "<br>";
$directory ->next();
}
?>
|
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__ ));
foreach ( $directory as $dir ) {
echo $directory ->key() . " => " .
$directory ->__toString() . "<br>";
}
?>
|
Output:
0 => .
1 => ..
2 => applications.html
3 => bitnami.css
4 => dashboard
5 => favicon.ico
6 => geeks.PNG
7 => gfg.php
8 => img
9 => index.php
10 => Sublime Text Build 3211 x64 Setup.exe
11 => webalizer
12 => xampp
Note: The output of this function depends on the content of server folder.
Reference: https://www.php.net/manual/en/directoryiterator.tostring.php
Last Updated :
14 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...