Jump to content

Archived

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

JustinMs66@hotmail.com

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]

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
Tom beat me to it, but that's how I would do it. Also, the reason that there is . and .. is because those are the directories you go to go back a directory or two.. so yeah you don't want to have those hanging around the public.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
k awesome :D thnx man!

but i need to know....what is the code for if u wana insert a PHP code into a HTML doc?

and i tried:

[code=php:0]<?php
//php code goes here
?>[/code]

but that didn't work AT all. so how do i do it?

Share this post


Link to post
Share on other sites
That will work if your HTML file is named "something.php". If it is named "something.html", the PHP processor will not get invoked.

Ken

Share this post


Link to post
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

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites
Use the function is_dir() http://www.php.net/is_dir to test whether the file is a directory or not.

Ken

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

×

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.