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
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS