Why does readdir() work in one place and not another?
Posted: Wed Apr 08, 2009 10:54 am
As a PHP newbie (but a long-time programmer), I'm trying to iterate through the files in a directory. I have a form with two buttons (Show and Upload). At this point, the code for both buttons just opens a directory with a hard-coded path, and iterates through the files. The code for the Show button works, but the code for Upload doesn't. Here's the essential code:
If I click the Show button, I see:
Show button was clicked
Path: ./images/ - Handle: Resource id #4
and the images are displayed (at least that part works). I copied this exact code to the block that responds to the Upload button. It failed with:
[Uploadbutton was clicked
Path: ./images/ - Handle: Resource id #4
Warning: readdir(): 4 is not a valid Directory resource in /home/xxx/yyy/store_image3.php on line 166
I then added code to close the directory and open it again:
Now when I click Upload instead of Show, I see:
Upload button was clicked
Path: ./images/ - Handle: Resource id #4
Closed and reopened. Path: ./images/ - dir_handle: Resource id #5
Warning: readdir(): 5 is not a valid Directory resource in /home/xxx/yyy/store_image3.php on line 170
I note that closedir() had no problem with $handle being a valid directory resource, only readdir().
Does anyone know why readdir() won't work with the Upload button? I'm completely befuddled why identical code with identical directory handles doesn't work identically.
Thanks in advance for any help.
Mike
Code: Select all
$handle = opendir($path) or die "oops!");
echo "<p>Show button was clicked</p>";
echo "<p>Path: $path - Handle: $handle</p>;
while ($file = readdir($handle)
{
// display the JPG image -- this works fine
}
closedir($handle);
Show button was clicked
Path: ./images/ - Handle: Resource id #4
and the images are displayed (at least that part works). I copied this exact code to the block that responds to the Upload button. It failed with:
[Uploadbutton was clicked
Path: ./images/ - Handle: Resource id #4
Warning: readdir(): 4 is not a valid Directory resource in /home/xxx/yyy/store_image3.php on line 166
I then added code to close the directory and open it again:
Code: Select all
$handle = opendir($path) or die "oops!");
echo "<p>Upload button was clicked</p>";
echo "<p>Path: $path - Handle: $handle</p>;
closedir($handle);
$handle = opendir($path) or die "oops!");
echo "<p>Closed and reopened. Path: $path - Handle: $handle</p>;
while ($file = readdir($handle)
{
// Just list the files for now
}
closedir($handle);
Now when I click Upload instead of Show, I see:
Upload button was clicked
Path: ./images/ - Handle: Resource id #4
Closed and reopened. Path: ./images/ - dir_handle: Resource id #5
Warning: readdir(): 5 is not a valid Directory resource in /home/xxx/yyy/store_image3.php on line 170
I note that closedir() had no problem with $handle being a valid directory resource, only readdir().
Does anyone know why readdir() won't work with the Upload button? I'm completely befuddled why identical code with identical directory handles doesn't work identically.
Thanks in advance for any help.
Mike