Listing Directories Glitch

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
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Listing Directories Glitch

Post by JustinMs66 »

ok so like right now, this code will display the directories in the "files" directory in a select box. but the first 2 options are "." and ".." (without quotes), and THEN the folders. so how do i get rid of those .. things. ???

here is my code:

Code: Select all

<?php
$dirpath = "files/"; 
$dlist = opendir($dirpath); 
$info = "<select name='dir'>";
while ($read = readdir($dlist)) {
$info.="<option value='$read'>$read</option>";
}
closedir($dlist); 
$info.="</select>";
echo $info;
?>
and it produces this:
Image
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Listing Directories Glitch HELP!!!!!!!! !!!! !!!!

Post by Christopher »

Code: Select all

while ($read = readdir($dlist)) {
     if (($read != '.') || ($read != '..')) {
          $info.="<option value='$read'>$read</option>";
     }
}
Or you could do:

Code: Select all

readdir($dlist);
readdir($dlist);
while ($read = readdir($dlist)) {
          $info.="<option value='$read'>$read</option>";
}
(#10850)
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

ok so i used this:

Code: Select all

<?php
$dirpath = "uploads/cats/"; 
$dlist = opendir($dirpath); 
$info = "<select name='dir'>";
readdir($dlist);
readdir($dlist);
while ($read = readdir($dlist)) {
          $info.="<option value='$read'>$read</option>";
}
closedir($dlist); 
$info.="</select>";
echo $info;
?>
and i JUST realzed that it shows FILES as WELL as directories. so how do i make it ONLY show directories?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

there's this wonderful function called is_dir() :)
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

ok so like what, would i do something like this?

would i replace this:
readdir($dlist);

with this?
readdir(is_dir($dlist));
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

Code: Select all

while ($read = readdir($dlist)) {
     if (is_dir($read) && $read != '.' && $read != '..') {
          $info.="<option value='$read'>$read</option>";
     }
}
those conditionals will have && in place of ||.
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

when i try that, the select box is empty. nothing exists.

and what about && ??
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

replace is_dir($read) with is_dir($dirpath.$read)
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

k that worked thnx :)
Post Reply