PHP | DirectoryIterator key() Function Last Updated : 26 Nov, 2019 Comments Improve Suggest changes Like Article Like Report The DirectoryIterator::key() function is an inbuilt function in PHP which is used to return the key for the current DirectoryIterator item. Syntax: string DirectoryIterator::key( void ) Parameters: This function does not accept any parameters. Return Value: This function returns the key for the current DirectoryIterator item. Below programs illustrate the DirectoryIterator::key() function in PHP: Program 1: php <?php // Create a directory Iterator $directory = new DirectoryIterator(dirname(__FILE__)); // Loop runs for each element of directory foreach($directory as $dir) { // Display the key and filename echo $dir->key() . " => " . $dir->getFilename() . "<br>"; } ?> Output: 0 => . 1 => .. 2 => applications.html 3 => bitnami.css 4 => dashboard 5 => favicon.ico 6 => geeks.PNG 7 => gfg.php 8 => img 9 => index.php 10 => Sublime Text Build 3211 x64 Setup.exe 11 => webalizer 12 => xampp Program 2: php <?php // Create a directory Iterator $directory = new DirectoryIterator(dirname(__FILE__)); // Loop runs while directory is valid while ($directory->valid()) { // Check for directory element if($directory->isDir()) { // Display the key and filename echo $directory->key() . " => " . $directory->getFilename() . "<br>"; } // Move to the next element $directory->next(); } ?> Output: 0 => . 1 => .. 4 => dashboard 8 => img 11 => webalizer 12 => xampp Note: The output of this function depends on the content of server folder. Reference: https://www.php.net/manual/en/directoryiterator.key.php Comment More infoAdvertise with us Next Article PHP | DirectoryIterator key() Function J jit_t Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-Iterators Similar Reads PHP | DirectoryIterator next() Function The DirectoryIterator::next() function is an inbuilt function in PHP which is used to move forward to the next DirectoryIterator item. Syntax: void DirectoryIterator::next( void ) Parameters: This function does not accept any parameters. Return Value: This function does not return any value. Below p 1 min read PHP | DirectoryIterator getType() Function The DirectoryIterator::getType() function is an inbuilt function in PHP which is used to check the type of the current DirectoryIterator item. Syntax: string DirectoryIterator::getType( void ) Parameters: This function does not accept any parameters. Return Value: This function returns a string whic 2 min read PHP | DirectoryIterator isLink() Function The DirectoryIterator::isLink() function is an inbuilt function in PHP which is used to check the current DirectoryIterator item is a symbolic link or not. Syntax: bool DirectoryIterator::isLink( void ) Parameters: This function does not accept any parameters. Return Value: This function returns TRU 1 min read PHP | DirectoryIterator isFile() Function The DirectoryIterator::isFile() function is an inbuilt function in PHP which is used to check the current DirectoryIterator item is a regular file or not. Syntax: bool DirectoryIterator::isFile( void ) Parameters: This function does not accept any parameters. Return Value: This function returns TRUE 1 min read 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 pa 2 min read Like