PHP | DirectoryIterator getPath() Function
The DirectoryIterator::getPath() function is an inbuilt function in PHP which is used to get the path of current Iterator item without filename.
Syntax:
string DirectoryIterator::getPath( void )
Parameters: This function does not accept any parameters.
Return Value: This function returns the file path, omitting the file name and any trailing slash.
Below programs illustrate the DirectoryIterator::getPath() function in PHP:
Program 1:
<?php
$directory = new DirectoryIterator(dirname( __FILE__ ));
echo $directory ->getPath();
?>
|
Output:
C:\xampp\htdocs
Program 2:
<?php
$directory = new DirectoryIterator(dirname( __FILE__ ));
foreach ( $directory as $dir ) {
$file = $directory ->current();
echo $dir ->key() . " => " .
$file ->getFilename() . " | Path: " .
$directory ->getPath() . "<br>" ;
}
?>
|
Output:
0 => . | Path: C:\xampp\htdocs
1 => .. | Path: C:\xampp\htdocs
2 => applications.html | Path: C:\xampp\htdocs
3 => bitnami.css | Path: C:\xampp\htdocs
4 => dashboard | Path: C:\xampp\htdocs
5 => favicon.ico | Path: C:\xampp\htdocs
6 => geeks.PNG | Path: C:\xampp\htdocs
7 => gfg.php | Path: C:\xampp\htdocs
8 => img | Path: C:\xampp\htdocs
9 => index.php | Path: C:\xampp\htdocs
10 => webalizer | Path: C:\xampp\htdocs
11 => xampp | Path: C:\xampp\htdocs
Note: The output of this function depends on the content of server folder.
Last Updated :
07 Mar, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...