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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
,ikedodd
Forum Newbie
Posts: 5
Joined: Wed Apr 08, 2009 9:40 am

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

Post 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
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

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

Post 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.
,ikedodd
Forum Newbie
Posts: 5
Joined: Wed Apr 08, 2009 9:40 am

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

Post 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
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

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

Post 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.
Post Reply