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
$var
Forum Contributor
Posts: 317 Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto
Post
by $var » Fri Mar 10, 2006 10:36 am
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
Burrito
Spockulator
Posts: 4715 Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah
Post
by Burrito » Fri Mar 10, 2006 10:46 am
there is no way to prevent opendir() from fetching them.
you can use a conditional within your loop though to filter those out.
$var
Forum Contributor
Posts: 317 Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto
Post
by $var » Fri Mar 10, 2006 11:05 am
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>";
}
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Mar 10, 2006 11:05 am
glob() doesn't return those two "special" directories.
Burrito
Spockulator
Posts: 4715 Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah
Post
by Burrito » Fri Mar 10, 2006 11:19 am
R4000
Forum Contributor
Posts: 168 Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom
Post
by R4000 » Fri Mar 10, 2006 11:27 am
Like he said, your best bet is:
Code: Select all
foreach(glob("*") as $filename){
echo $filename."<br />\n";
}