Jump to content


Photo

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


  • Please log in to reply
12 replies to this topic

#1 JustinMs66@hotmail.com

JustinMs66@hotmail.com
  • Members
  • PipPipPip
  • Advanced Member
  • 138 posts

Posted 09 September 2006 - 09:19 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:
<?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:
http://www.csscobalt..._php_v01_01.bmp


#2 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 09 September 2006 - 09:57 PM

Here is an example from the manual on readdir

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

Hope this helps,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#3 tleisher

tleisher
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts

Posted 09 September 2006 - 09:57 PM

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.

#4 JustinMs66@hotmail.com

JustinMs66@hotmail.com
  • Members
  • PipPipPip
  • Advanced Member
  • 138 posts

Posted 10 September 2006 - 06:13 AM

wul when i do it this way, it dosn't come up in a select box. it comes up in text. how do i make it come up in a select box, like in my above code?

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 September 2006 - 06:18 AM

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

Ken

#6 JustinMs66@hotmail.com

JustinMs66@hotmail.com
  • Members
  • PipPipPip
  • Advanced Member
  • 138 posts

Posted 10 September 2006 - 06:22 AM

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:

<?php
//php code goes here
?>

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

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 September 2006 - 06:24 AM

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

#8 JustinMs66@hotmail.com

JustinMs66@hotmail.com
  • Members
  • PipPipPip
  • Advanced Member
  • 138 posts

Posted 10 September 2006 - 06:25 AM

well it's named "something.HTML" so is there a way to insert PHP into a html doc?

#9 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 September 2006 - 06:31 AM

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

#10 JustinMs66@hotmail.com

JustinMs66@hotmail.com
  • Members
  • PipPipPip
  • Advanced Member
  • 138 posts

Posted 10 September 2006 - 06:45 AM

ok so i used this:

  <?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 realized that it shows FILES as WELL as directories. so how do i make it ONLY show directories?

#11 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 September 2006 - 06:47 AM

Use the function is_dir() http://www.php.net/is_dir to test whether the file is a directory or not.

Ken

#12 JustinMs66@hotmail.com

JustinMs66@hotmail.com
  • Members
  • PipPipPip
  • Advanced Member
  • 138 posts

Posted 10 September 2006 - 06:49 AM

ok so like what, would i do something like this?

would i replace this:
readdir($dlist);

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

#13 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 10 September 2006 - 07:37 AM

No. Try something like this.

  <?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;
?>

Hope this helps,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users