Jump to content

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


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
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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.