Tuesday, September 16, 2025
HomeLanguagesPHP | DirectoryIterator seek() Function

PHP | DirectoryIterator seek() Function

The DirectoryIterator::seek() function is an inbuilt function in PHP which is used to seek the DirectoryIterator item to the given position.

Syntax:

void DirectoryIterator::seek( int $position )

Parameters: This function accept single parameter $position which holds the zero-based numeric position to seek the element.

Return Value: This function does not return any value.

Below programs illustrate the DirectoryIterator::seek() function in PHP:

Program 1:




<?php
  
// Create a directory Iterator
$directory = new DirectoryIterator(dirname(__FILE__));
  
// Move to the third element (0 based indexing)
$directory->seek(2);
  
// Check for validity of element
if($directory->valid()) {
  
    // Display the filename
    echo $directory->getFilename();
}
  
?>


Output:

applications.html

Program 2:




<?php
  
// Create a directory Iterator
$directory = new DirectoryIterator(dirname(__FILE__));
  
// Move to the third element (0 based indexing)
$directory->seek(2);
  
// Check for validity of element
if($directory->valid()) {
  
    // Display the key and filename
    echo $directory->key() . " => " .
    $directory->getFilename();
}
  
?>


Output:

2 => applications.html

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

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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32293 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6656 POSTS0 COMMENTS
Nicole Veronica
11826 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11892 POSTS0 COMMENTS
Shaida Kate Naidoo
6773 POSTS0 COMMENTS
Ted Musemwa
7044 POSTS0 COMMENTS
Thapelo Manthata
6729 POSTS0 COMMENTS
Umr Jansen
6739 POSTS0 COMMENTS