Readdir problem

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
nikosb
Forum Newbie
Posts: 8
Joined: Sat Sep 23, 2006 5:46 pm

Readdir problem

Post by nikosb »

Iam having problems trying to read the contents of a directory. This is the code that I use:

Code: Select all

<HTML>
<HEAD><TITLE>TEST</TITLE></HEAD>

<BODY>
<?php
$address = $_GET['wb'];

if($dire = opendir($address));
{
  print("Directory name: $address<BR>\n");
  while(false !== ($file=readdir($dire)) );
  {
     print("$file<BR>\n");
  }
  closedir($dire)
}

?>
</BODY>
</HTML>
I get the directory from a form in an html file using the GET method. I have tested this script using various directories in my server and even the simplest one (. current directory) does not work. You can test the code yourselves at:
http://www.eng.umd.edu/enme331/wpc.html

It seems that the directory is valid because the code goes through the opendir statement and prints the directory name. It also seems to go into the while loop once because there are two <BR> printed. However the $file variable obtained from the readdir is empty.
I have checked two things:
1. The local (.) directory itself contains about 8 files and in the directory all the files have read permissions (-rw-r--r--)
2. The server allows directory reading (allow_url_fopen is ON, http://www.eng.umd.edu/enme331/info.php)

Any idea why I cannot read the contents of a directory? What am I doing wrong?

Thank you,

Nikolaos
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Seems a bit odd. Given that code, it should work. If it gets past the if(opendir), then I don't see why it's not reading the contents. Does the directory itself have read permissions?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
nikosb
Forum Newbie
Posts: 8
Joined: Sat Sep 23, 2006 5:46 pm

Post by nikosb »

Yes, the directory has also read access, the same as the files that are into it. I was able to list the contents of the directory using the folloing code:

Code: Select all

<?php
$d = dir("$address");
echo "Handle: " . $d->handle . "\n";
echo "Path: " . $d->path . "\n";
while (false !== ($entry = $d->read())) {
   echo $entry."\n";
}
$d->close();
?>
However I don't think the dir class supports URL addresses. What more can I check?

Thank you,

Nikolaos
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

nikosb wrote:while(false !== ($file=readdir($dire)) );
remove the semicolon
nikosb
Forum Newbie
Posts: 8
Joined: Sat Sep 23, 2006 5:46 pm

Post by nikosb »

Thanks for the suggestion. It works now without the semicolumn. But why was the semicolumn creating this problem?
Also, how can I enter a URL address as a string in the opendir function? If I enter a URL as the $address variable then the if($dire=opendir($address)) expression does not evaluate to true. I believe the opendir function can accept a URL. Is there anything more I have to specify in the opendir?

Thank you,

Nikolaos
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

; is an empty statement. Therefore
while(false !== ($file=readdir($dire)) );
means: while there are more files to read (name assigned to $file) do nothing.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Odd how the php parser didn't catch that, you'd think.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Jcart wrote:Odd how the php parser didn't catch that, you'd think.
As before, it's a completely legal statement. There's nothing to catch.
Post Reply