Page 1 of 1

Listing Directories Glitch

Posted: Sat Sep 09, 2006 4:31 pm
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

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

Posted: Sat Sep 09, 2006 5:52 pm
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>";
}

Posted: Sun Sep 10, 2006 1:44 am
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?

Posted: Sun Sep 10, 2006 1:48 am
by feyd
there's this wonderful function called is_dir() :)

Posted: Sun Sep 10, 2006 1:50 am
by JustinMs66
ok so like what, would i do something like this?

would i replace this:
readdir($dlist);

with this?
readdir(is_dir($dlist));

Posted: Sun Sep 10, 2006 2:04 am
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 ||.

Posted: Sun Sep 10, 2006 2:20 am
by JustinMs66
when i try that, the select box is empty. nothing exists.

and what about && ??

Posted: Sun Sep 10, 2006 2:26 am
by n00b Saibot
replace is_dir($read) with is_dir($dirpath.$read)

Posted: Sun Sep 10, 2006 2:34 am
by JustinMs66
k that worked thnx :)