Monday, June 15, 2026
HomeLanguagesPHP | DirectoryIterator getPathname() Function

PHP | DirectoryIterator getPathname() Function

The DirectoryIterator::getPathname() function is an inbuilt function in PHP which is used to return the path and file name of the current DirectoryIterator item.

Syntax:

string DirectoryIterator::getPathname( void )

Parameters: This function does not accept any parameters.

Return Value: This function returns the path and file name of current file. The directories do not contain the trailing slash.

Below programs illustrate the DirectoryIterator::getPathname() function in PHP:

Program 1:




<?php
  
// Create a directory Iterator
$directory = new DirectoryIterator(dirname(__FILE__));
  
// Loop runs for each element of directory
foreach($directory as $dir) {
      
    // Display the path name
    echo $directory->getPathname() . "<br>";
}
?>


Output:

C:\xampp\htdocs\.
C:\xampp\htdocs\..
C:\xampp\htdocs\applications.html
C:\xampp\htdocs\bitnami.css
C:\xampp\htdocs\dashboard
C:\xampp\htdocs\favicon.ico
C:\xampp\htdocs\neveropen.PNG
C:\xampp\htdocs\gfg.php
C:\xampp\htdocs\img
C:\xampp\htdocs\index.php
C:\xampp\htdocs\webalizer
C:\xampp\htdocs\xampp

Program 2:




<?php
  
// Create a directory Iterator
$directory = new DirectoryIterator(dirname(__FILE__));
  
// Loop runs while directory element is valid
while ($directory->valid()) {
  
    // Check for directory element
    if ($directory->isDir()) {
  
        // Display the path name
        echo $directory->getPathname() . "<br>";
    }
  
    // Move to the next element
    $directory->next();
}
  
?>


Output:

C:\xampp\htdocs\.
C:\xampp\htdocs\..
C:\xampp\htdocs\dashboard
C:\xampp\htdocs\img
C:\xampp\htdocs\webalizer
C:\xampp\htdocs\xampp

Note: The output of this function depends on the content of server folder.

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

RELATED ARTICLES

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS