Directory listing

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand

Postby Joel » Sat Jun 21, 2003 7:14 pm

this is a code I use for listing a directory. I dunno how you could list things by certain criteria, this lists everything.

Code: Select all

//define the path as relative
  $path = ".";

  //using the opendir function
  $dir_handle = @opendir($path) or die("Unable to open $path");
  //make string with 70 spaces
  $space="                                 ";
  echo "<PRE>";
  echo "Directory Listing of $path\n";
  //running the while loop
  while ($file = readdir($dir_handle)) {
        // filename - output left-justified
        $t="<a href=$file>$file</a>";
        echo $t.substr($space,0,40-strlen($file))  ;
        // filesize - output right-justified
        $t=sprintf("%01.2f",$t)."kb ";
        echo substr($space,0,10-strlen($t)) . $t ;
        // filedate - output left-justified
        $t=date("d.M Y H:i:s", filemtime($file));
        echo $t.substr($space,0,20-strlen($file)) ;
        echo "\n";
  //closing the directory
  echo "</PRE>";


User avatar
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA

Postby Redcircle » Sun Jun 22, 2003 10:39 pm

this is my read in directory function

Code: Select all

   function read_in_dir($changedir, $file_or_dir)
      if ($dir = opendir($changedir))
         $fileArray = array();
         $dirArray = array();
         while($file = readdir($dir))
            if($file!=".." && $file!=".")
                  $fileArray[] = $file;
                  $dirArray[] = $file;
      if($file_or_dir == 'file')
      return $fileArray;
      if($file_or_dir == 'dir')
      return $dirArray;

basically to use this you put in the path on your server and specify wether you want to get directory names or file names.

Code: Select all

$arr = read_in_dir("/home/username/www/images","file");

You would then extract the data you are looking for from the array.

Code: Select all

 echo '<table>';
   foreach($arr as $key=>$value)
    echo '<tr><td>'.$value.'</td></tr>';
  echo '</table>';

I'm kinda fuzzy on what you meant by "contents of the 13th table cell "

Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest