Thursday, September 4, 2025
HomeLanguagesPHP | DirectoryIterator next() Function

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 programs illustrate the DirectoryIterator::next() function in PHP:

Program 1:




<?php
  
// Create a directory Iterator
$directory = new DirectoryIterator(dirname(__FILE__));
  
// Loop runs while directory is valid
while ($directory->valid()) {
      
    // Check for directory
    if($directory->isDir()) {
  
        // Display key and file name
        echo $directory->key() . " => "
        $directory->getFilename() . "<br>";
    }
  
    // Move to the next element
    $directory->next();
}
  
?>


Output:

0 => .
1 => ..
4 => dashboard
8 => img
11 => webalizer
12 => xampp

Program 2:




<?php
  
// Create a directory Iterator
$directory = new DirectoryIterator(dirname(__FILE__));
  
// Loop runs while directory is valid
while ($directory->valid()) {
      
    // Check for file element
    if($directory->isFile()) {
  
        // Display the filename
        echo $directory->getFilename() . "<br>";
    }
  
    // Move to the next element
    $directory->next();
}
  
?>


Output:

applications.html
bitnami.css
favicon.ico
neveropen.PNG
gfg.php
index.php
Sublime Text Build 3211 x64 Setup.exe

Note: The output of this function depends on the content of server folder.

Reference: https://www.php.net/manual/en/directoryiterator.next.php

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32262 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11857 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6696 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS