|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2000-07-08 11:53 UTC] johan at ekenberg dot se
I still experience problems with readdir(), using Apache 1.3.12 and PHP 4.0.1pl2:
The while loop exists at the first evaluation of the if-statement. If the if-statement is commented out, the code works.
$dir = opendir(".");
while ($file = readdir($dir)) {
print "<br>$file";
if ($file == "foo") {
// anything
}
}
Config:
PHP Version 4.0.1pl2
'./configure' '--with-apache=../apache_1.3.12' '--with-mysql' '--with-gd' '--with-ttf' '--with-ftp' '--with-swf' '--with-mcrypt' '--with-mhash' '--with-xml' '--disable-posix-threads' '--enable-memory-limit=yes' '--enable-track-vars' '--enable-debug=no'
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 02:00:01 2025 UTC |
1. The while loop is only evaluated once, despite the fact that there are more directory entries to list. I earlier wrote that the while-loop exits at the first evaluation of the if-statement, but that is not true. The execution continues after the if-statement until the end of the while loop, there it exits. If the if-statement is removed or commented out, the while loop will continue looping until all entries in the directory are listed. With the if-statement left in place it's exactly like putting "break;" as the last line of the while loop - it never evaluates more than once. I guess that something in the if-statement makes readdir($dir) evaluate to false the next time. $dir = opendir("."); while ($file = readdir($dir)) { print "<br>$file"; if ($file == "foo") { // anything or nothing here, doesn't matter } // Code placed here will also be evaluated, // but as long as the if-statement above is not // removed, the while loop will exit after // completing once - like putting a // break; // as the last line of the while loop. } I earlier submitted an URL with a working example which I believe displays the issue rather clearly: http://www.ekenberg.se/readdir/index.php 2. "gdb -X httpd" doesn't report anything unusual - httpd happily keeps on executing, there are no errors and it doesn't exit. zend_bailout() is never called.Stefan Kirch [s.kirch at bauer-kirch.de] mailed me: -- start quote -- I have the same problem (#5535) and found a "work-around" like the following: if (ereg("^foo$", $file)) { RegEx works fine, so it seems to be a problom of string-comparison. Codes like if ($x == "5") { also breaks the loop, on the other hand, numerical comparisons like if ($x == 5) { works fine. -- end quote -- I don't know if this might give any clues to the experts?You don't use closedir() anywhere in your code. Please try this example: <?php $dir = opendir("."); while ($file = readdir($dir)) { if ($file != "." && $file != "..") { echo "$file<br>"; } } closedir($dir); ?> If this doesn't help, upgrade to latest CVS. This works just fine for me. --Jani