FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Reading Directories





Assiez
Currently I have the following code to read the contents of a directory. I have two questions that I would appreciate being answered.

Current Code:

Code:
<html>
<head>
<title>Directory test</title>
</head>
<body>
<?php
   $dirname = "pictures/art/";
   $dh = opendir( $dirname ) or die("couldn't open directory");

   while ( $file = readdir( $dh ) ) {
      if ( is_dir( "$dirname/$file" ) )
         print " ";
      print "$file<br>";
   }
   closedir( $dh );
?>
</body>
</html>





Firstly, the script outputs something like the following:
Quote:
.
..
a folder
another folder
file.ext
HouseTop.JPG
P1010783.JPG
P1010785.JPG
P1010786.JPG
P1010788.JPG
P1010789.JPG


My question, what does the . and the .. mean?

Secondly, I have some subfolders in that directory, how can I have the script read those too?


Thanks in advance.
Assiez
I have found the following function which works. Does anyone have a simpler solution?

Quote:

Code:

   function searchdir ( $path , $maxdepth = -1 , $mode = "FULL" , $d = 0 )
   {
      if ( substr ( $path , strlen ( $path ) - 1 ) != '/' ) { $path .= '/' ; }     
      $dirlist = array () ;
      if ( $mode != "FILES" ) { $dirlist[] = $path ; }
      if ( $handle = opendir ( $path ) )
      {
          while ( false !== ( $file = readdir ( $handle ) ) )
          {
         if ( $file != '.' && $file != '..' )
         {
             $file = $path . $file ;
             if ( ! is_dir ( $file ) ) { if ( $mode != "DIRS" ) { $dirlist[] = $file ; } }
             elseif ( $d >=0 && ($d < $maxdepth || $maxdepth < 0) )
             {
            $result = searchdir ( $file . '/' , $maxdepth , $mode , $d + 1 ) ;
            $dirlist = array_merge ( $dirlist , $result ) ;
             }
          }
          }
          closedir ( $handle ) ;
      }
      if ( $d == 0 ) { natcasesort ( $dirlist ) ; }
      return ( $dirlist ) ;
   }


kv
Here is a simpler version

Code:

<html>
<head>
<title>Directory test</title>
</head>
<body>
<?php
function recurseDir($dirname)
{
   print "Directory: $dirname";
   $dh = opendir( $dirname ) or die("couldn't open directory");

   while ( $file = readdir( $dh ) )
   {
      if ( is_dir( "$dirname/$file" ) )
      {
   if( $file!="." && $file!="..")
     recurseDir("$dirname/$file");
      }
      else
   print "$file<br>";
   }
   closedir( $dh );
}

recurseDir("pictures/art");

?>
</body>
</html>


"." represents current directory and ".." represents parent of current directory.
Assiez
Thanks, but I think I'll stay with the current one because it allows me to set the max depth and such.
Related topics
Basic Search Engine Optimizing
[OFFICIAL] What are you currently reading?
Password Protecting Directories?
Tutorial: Image Rollovers w/ Javascript
An idea to stop people NOT reading TOS.
Your favourite book (official)
Fix for deleting files/directories owned by apache
Reading into the reading too much?
Problem with avatar in pbgpBB >>> read/write permis
Are christians educated?
ahh i hate reading
Problem with linux fedora core 4
Submitting to all these Web Directories.... any Advantages?
25 year old BSD bug fixed :p
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.