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
JustinMs66
Forum Contributor
Posts: 127 Joined: Sun Sep 03, 2006 4:18 pm
Post
by JustinMs66 » Sat Sep 09, 2006 4:31 pm
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:
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Sat Sep 09, 2006 5:52 pm
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)
JustinMs66
Forum Contributor
Posts: 127 Joined: Sun Sep 03, 2006 4:18 pm
Post
by JustinMs66 » Sun Sep 10, 2006 1:44 am
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?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Sep 10, 2006 1:48 am
there's this wonderful function called
is_dir()
JustinMs66
Forum Contributor
Posts: 127 Joined: Sun Sep 03, 2006 4:18 pm
Post
by JustinMs66 » Sun Sep 10, 2006 1:50 am
ok so like what, would i do something like this?
would i replace this:
readdir($dlist);
with this?
readdir(is_dir($dlist));
n00b Saibot
DevNet Resident
Posts: 1452 Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:
Post
by n00b Saibot » Sun Sep 10, 2006 2:04 am
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
|| .
JustinMs66
Forum Contributor
Posts: 127 Joined: Sun Sep 03, 2006 4:18 pm
Post
by JustinMs66 » Sun Sep 10, 2006 2:20 am
when i try that, the select box is empty. nothing exists.
and what about && ??
n00b Saibot
DevNet Resident
Posts: 1452 Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:
Post
by n00b Saibot » Sun Sep 10, 2006 2:26 am
replace is_dir($read) with is_dir($dirpath.$read)
JustinMs66
Forum Contributor
Posts: 127 Joined: Sun Sep 03, 2006 4:18 pm
Post
by JustinMs66 » Sun Sep 10, 2006 2:34 am
k that worked thnx