Page 1 of 1

readdir help

Posted: Fri Mar 10, 2006 10:36 am
by $var
i am trying to open a directory with this snippet here:

Code: Select all

if ($handle = opendir($path)) {
		   while (false !== ($file = readdir($handle))) {
			   echo "<a href='$path2$file' target='_blank'>";
			   echo "$file";
			   echo "</a><br>";
		   }
		   closedir($handle);
		}
and it works, but i get 2 levels of directory navigation above the files in the directory, is there a way to not get them?
see:
.
..
img_102.gif
img_103.gif
img_62.gif

Posted: Fri Mar 10, 2006 10:46 am
by Burrito
there is no way to prevent opendir() from fetching them.

you can use a conditional within your loop though to filter those out.

Posted: Fri Mar 10, 2006 11:05 am
by $var
i've tried a few different condition, and though the string length is echoing as 1 and 2 for these, it is still showing, despite the if

Code: Select all

while (false !== ($file = readdir($handle))) {
			   $val = "4";
			   if (strlen($file) < $val){
			   echo "";
			   }
			   echo "<a href='$path2$file' target='_blank'>";
			   echo "$file";
			   echo "</a><br>";
		   }

Posted: Fri Mar 10, 2006 11:05 am
by feyd
glob() doesn't return those two "special" directories.

Posted: Fri Mar 10, 2006 11:19 am
by Burrito

Code: Select all

if($file != "." && $file != "..")
8O

Posted: Fri Mar 10, 2006 11:27 am
by R4000
Like he said, your best bet is:

Code: Select all

foreach(glob("*") as $filename){
 echo $filename."<br />\n";
}