php.net |  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
Votes:2
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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ai0867 at gmail dot com
New email:
PHP Version: OS:

 

 [2012-10-03 09:30 UTC] ai0867 at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/function.readdir#refsect1-function.readdir-
returnvalues
---
readdir() should return the boolean value false on failure. When calling it on a 
failed opendir() result (false), it returns NULL instead.

http://www.php.net/manual/en/functions.internal.php
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:
----------------
false

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
NULL

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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] krakjoe@php.net
-Status: Open +Status: Not a bug
 [2012-11-15 10:11 UTC] krakjoe@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

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-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 17:01:28 2024 UTC