Tuesday, November 19, 2024
Google search engine
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

Recent Comments