readdir help

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
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

readdir help

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post 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>";
		   }
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

glob() doesn't return those two "special" directories.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Code: Select all

if($file != "." && $file != "..")
8O
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post by R4000 »

Like he said, your best bet is:

Code: Select all

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