|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #63205 readdir() failure returns NULL instead of false
Submitted: 2012-10-03 09:30 UTC Modified: 2012-11-16 05:21 UTC
Avg. Score:4.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:0 (0.0%)
From: ai0867 at gmail dot com Assigned:
Status: Not a bug Package: Directory function related
PHP Version: 5.3.17 OS: Debian Squeeze
Private report: No CVE-ID: None
 [2012-10-03 09:30 UTC] ai0867 at gmail dot com
From manual page:
readdir() should return the boolean value false on failure. When calling it on a 
failed opendir() result (false), it returns NULL instead.
This indicates that this is intentional, but the readdir() documentation says 
nothing about this.

Test script:
$dir = opendir("/tmp/this-dir-does-not-exist");
$item = readdir($dir);
var_dump($item); // Should be false. Returns NULL on my machine

Expected result:

Actual result:
PHP Warning:  opendir(/tmp/this-dir-does-not-exist): failed to open dir: No such 
file or directory in /home/ai/test.php on line 2
PHP Warning:  readdir() expects parameter 1 to be resource, boolean given in 
test.php on line 3


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-10-19 12:02 UTC] joko at mms-dresden dot de
The error also occurs on RHEL 5.8 using PHP versions 5.3.15, 5.3.16 and 5.3.17. I 
was not able to test other versions.

Since max_execution_time does not apply to readdir, the following code (present 
in Typo3) will trigger an endless loop which 
continues to run even after the user has aborted the HTTP request.

while (($file = readdir($handle)) !== FALSE) {
 [2012-10-25 13:22 UTC] inscriptions at starbender dot com
I also have the same problem with PHP-5.3.17 on SuSE
 [2012-11-15 10:11 UTC]
-Status: Open +Status: Not a bug
 [2012-11-15 10:11 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

Conventions established; it is the responsibility of the programmer to ascertain 
that the parameters they pass to a function or method are valid. The programmer 
should handle the return of the call to opendir without the assumption that the 
call succeeded.
 [2012-11-16 05:21 UTC] ai0867 at gmail dot com
The documentation still says nothing about returning NULL, while telling the 
programmer to differentiate between FALSE and all other values.

I did file this bug as a documentation problem.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat Jun 19 22:01:22 2021 UTC