displaying dir contents

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

Post Reply
liquid
Forum Newbie
Posts: 14
Joined: Thu Oct 21, 2004 2:19 pm
Location: Wyoming USA

displaying dir contents

Post by liquid »

Im trying to display a dir's contents. Which is working fine except that it is also displaying the dir structure:

example:
. <--- do not want this to display
.. <--- do not want this to display
first file
second file
etc...

How do I get rid of those first entries????

Code: Select all

<?php
$dir = "fileupload/uploads/".$row['cname']."/";
				if (is_dir($dir)) {
  					 if ($dh = opendir($dir)) {
							do{  
						 	echo "<a href="$dir$file" target="_blank">";
          					 echo $file;
							 echo "</a>";
							 echo "<br>";
							
							}while(($file = readdir($dh)) !== false);
      					 
      				 closedir($dh);
   					}
				}

Code: Select all

<?php
?>
?>

Thanks in advance!
liquid
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

just make sure the file does not equal '.' or '..'
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

Code: Select all

<?php

if ($handle = opendir($dir)) {
    while (false !== ($filename = readdir($handle))) {

        if ($filename !== '.' && $filename !== '..') {
           echo $filename;
        }

    }
    closedir($handle);
}

?>
liquid
Forum Newbie
Posts: 14
Joined: Thu Oct 21, 2004 2:19 pm
Location: Wyoming USA

Post by liquid »

OMG...duh.. I cant believe I didnt think of that! Time to go home. :oops:

Thanks much for the quick reply!!
Post Reply