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
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