Jump to content

Listing Directories Glitch HELP!!!!!!!! !!!! !!!!


JustinMs66@hotmail.com

Recommended Posts

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]<?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;
?>[/code]

and it produces this:
[img]http://www.csscobalt.com/uploads/help_php_v01_01.bmp[/img]
Link to comment
https://forums.phpfreaks.com/topic/20247-listing-directories-glitch-help/
Share on other sites

Here is an example from the manual on [url=http://us3.php.net/manual/en/function.readdir.php]readdir[/url]

[code]
<?php
if ($handle = opendir('.')) {
  while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != "..") {
          echo "$file\n";
      }
  }
  closedir($handle);
}
?> [/code]

Hope this helps,
Tom
Just replace these lines in your script:
[code]<?php
while ($read = readdir($dlist)) {
$info.="<option value='$read'>$read</option>";
}
?>[/code]
with
[code]<?php
while (false !== ($read = readdir($dlist))) {
    if ($read != "." && $read != "..") $info.="<option value='$read'>$read</option>";
}?>[/code]

Ken
Rename it to "something.php".

If you're using the Apache web server and have access to the httpd.conf file, you can modify it to make all *.html files go through PHP first, but that is overkill.

Use Javascript with AJAX to invoke your PHP script and return what you want to insert, then the Javascript can insert it into the document.

It's best just to rename the file.

I've gotten into the habit of naming all my files something.php, so I don't have to deal with this problem, even if there is no PHP in them at all, which is very rare these days.

Ken
ok so i used this:

[code]  <?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;
?>[/code]

and i JUST realized that it shows FILES as WELL as directories. so how do i make it ONLY show directories?
No. Try something like this.

[code]
  <?php
$dirpath = "uploads/cats/";
$dlist = opendir($dirpath);
$info = "<select name='dir'>";
while (false !== ($read = readdir ($dlist))) {
     if ($read != '.' && $read != '..' && is_dir($read)) {
          $info.="<option value='$read'>$read</option>";
     }
}
closedir($dlist);
$info.="</select>";
echo $info;
?>[/code]

Hope this helps,
Tom

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.