Page 1 of 1

Readdir problem

Posted: Wed Oct 25, 2006 12:36 am
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

Posted: Wed Oct 25, 2006 1:33 am
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?

Posted: Wed Oct 25, 2006 9:37 am
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

Posted: Wed Oct 25, 2006 9:58 am
by volka
nikosb wrote:while(false !== ($file=readdir($dire)) );
remove the semicolon

Posted: Wed Oct 25, 2006 11:40 am
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

Posted: Wed Oct 25, 2006 11:45 am
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.

Posted: Wed Oct 25, 2006 1:16 pm
by John Cartwright
Odd how the php parser didn't catch that, you'd think.

Posted: Wed Oct 25, 2006 3:11 pm
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.