Listing files in directories

From: Date: Mon, 18 Sep 2000 20:17:41 +0000
Subject: Listing files in directories
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-17194@lists.php.net to get a copy of this message
I am working on a script that will display all files in your home directory, and if a file is a directory, it is presented as a link that you can select and submit the name of the link back to the script. I expect what I have so far will only work for the first two levels. Where I am stuck is determining if there are any files in a directory before I attempt to list them. Does opendir() return a value that can be tested? The manual states that opendir() returns a directory handle as an integer. <?php if(!$startdir) {$startdir='home';} echo ("<font size=5><b>Directory: $startdir</b></font>\n"); if($startdir == "home") {$startdir='.';} else { echo("<p><a href=\"files.php\"><b>Return Home</b></a><p>\n"); } $handle=opendir($startdir); if($handle!=""){ echo ("<p><font size=4><b>Files:</b></font><p>\n"); while (($file = readdir($handle))!==false) {
      if (substr($file,0,1)!='.'){
        if(is_dir($file)){
echo("<a href=\"files.php?startdir=" . $file . "\">" . $file . "</a><br>\n");
      }
        else { echo("$file<br>\n"); }
      }
} else { echo("<b>This directory is empty</b>\n");} } closedir($handle); ?> Next I need to tackle passing on the path for directories more than one deep. -- _/ _/ _/ _/ Michael Collins _/ _/ _/ Equinox Enterprises _/ _/ Seattle, WA, USA _/ mcollins206@home.com

« previous php.general (#17194) next »