Saturday, February 7, 2026
HomeLanguagesPHP | DirectoryIterator getPath() Function

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
  
// Create a directory Iterator
$directory = new DirectoryIterator(dirname(__FILE__));
  
// Display the path
echo $directory->getPath();
?> 


Output:

C:\xampp\htdocs

Program 2:




<?php
  
// Create a directory Iterator
$directory = new DirectoryIterator(dirname(__FILE__));
  
// Loop runs for each element of directory
foreach($directory as $dir) {
      
    $file = $directory->current();
      
    // Display key, filename and its path
    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 => neveropen.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.

Reference: https://www.php.net/manual/en/directoryiterator.getpath.php

RELATED ARTICLES

Most Popular

Dominic
32491 POSTS0 COMMENTS
Milvus
126 POSTS0 COMMENTS
Nango Kala
6863 POSTS0 COMMENTS
Nicole Veronica
11987 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12076 POSTS0 COMMENTS
Shaida Kate Naidoo
6996 POSTS0 COMMENTS
Ted Musemwa
7238 POSTS0 COMMENTS
Thapelo Manthata
6947 POSTS0 COMMENTS
Umr Jansen
6933 POSTS0 COMMENTS