Page 1 of 1

Why does readdir() work in one place and not another?

Posted: Wed Apr 08, 2009 10:54 am
by ,ikedodd
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:

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);
 
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:

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

Re: Why does readdir() work in one place and not another?

Posted: Wed Apr 08, 2009 12:38 pm
by php_east
i don't at this instant have an answer for your question, but it baffles me how you can get away with not correctly opening and closing brackets.

Re: Why does readdir() work in one place and not another?

Posted: Wed Apr 08, 2009 6:44 pm
by ,ikedodd
php_east wrote: while ($file = readdir($handle)

i don't at this instant have an answer for your question, but it baffles me how you can get away with not correctly opening and closing brackets.
Naturally that should be while ($file = readdir($handle)) {...}

It was my typo because this forum doesn't let me paste from the clipboard into a message, so I had to type it by hand. The actual PHP code is correct, as evidenced by no parse or other errors when the page is served.

Can someone tell me how to paste into a message here?

Or, better still, why readdir() works in one place, but not in another?

Mike

Re: Why does readdir() work in one place and not another?

Posted: Wed Apr 08, 2009 10:00 pm
by php_east
your codes run fine on my localhost setup.
what sort of server hosting are you having this problem with. could it be virtual hosts ?

it maybe that the current directory isn't the one you want it to be on.
PHP does not gurantee readdir always points to the same directory if you use relative dir.
i would suggest you use getcwd() to see if it's actually pointing to where you want it to.