|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70561 DirectoryIterator::seek should throw OutOfBoundsException
Submitted: 2015-09-23 14:01 UTC Modified: 2015-10-29 09:56 UTC
Avg. Score:3.7 ± 0.5
Reproduced:1 of 2 (50.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: Assigned: tyrael (profile)
Status: Closed Package: SPL related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2015-09-23 14:01 UTC]
Originally reported on SO:

DirecotryIterator implements SeekableIterator, which "should throw an OutOfBoundsException if the position is not seekable".  However, the DirectoryIterator implementation simply returns when the position cannot be sought.

This leads to developer confusion, like:

So how would one reasonably expect to know whether to use valid() to confirm valid position after seek($position) while also anticipating that the seek() might throw an Exception instead of updating the position, so that valid() returns true?

Related to

Test script:
$dir_iterator = new DirectoryIterator(dirname(__FILE__));
try {
    $dir_iterator->seek(500);  // arbitrarily high seek position
} catch (OutOfBoundsException $e) {
    echo $e->getMessage() . PHP_EOL;
echo $dir_iterator->key() . PHP_EOL;
echo "Is valid? " . (int) $dir_iterator->valid() . PHP_EOL;

Expected result:
Seek position 500 is out of range
Is valid? 0

Actual result:
Is valid? 0


Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-10-29 09:56 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: tyrael
 [2015-10-29 09:56 UTC]
The fix for this bug has been committed.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at

 For Windows:
Thank you for the report, and for helping us make PHP better.

the fix should be released with 5.6.16
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Sep 23 04:01:25 2020 UTC