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

Recent Comments