Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

display files in directory 2

Status
Not open for further replies.

calv1n

Programmer
Sep 29, 2007
18
CA
How do I display the files of a directory, but just the files, not any subdirectories? Thanks.

-CALV1N
 
Lookup the following PHP functions

opendir()
readdir()
is_file()
is_dir()

That should get you started.

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
well then you've got all the info you need! you need to read the notes to each of the functions too but in this case the example to readdir would have given you a great script and the function cross reference of is_dir would have finished it off for you.

Code:
$dir = ''; //put your directory info here
$dh = opendir($dir);
while (false !== ($file = readdir($dh))){
 if (!is_dir($file)){echo $file . "<br/>";} 
}
 
Okay, I got it to work:

Code:
		if ($handle = opendir($loc)) {
          		while (false !== ($file = readdir($handle))) {
                  		if (is_dir($loc.$file)) continue;
                  			if ($file != "." && $file != "..") {

                      				$thelist .= "$file";
                  			}
               			}
			}
      		echo $thelist;

Now, how do I display all directories, including subdirectories (no files this time :))?

The code I'm using right now is:

Code:
                echo "<option value=\"\" selected>----------------------------</option>";
                $dir = 'content/';
		$handle = opendir($dir);
		while(($item = readdir($handle)) !== false) {
  			if(( $item != '.' ) && ( $item != '..' ) && is_dir($dir.$item)) {
                                // DISPLAY DIRECTORIES
                        	echo "<option value=\"$item\">$item</option>";
  			}
		}

Thanks.

-CALV1N
 
Now, how do I display all directories, including subdirectories (no files this time smile)?

You create a recursive function that uses the PHP functions shown above and do a check on each 'thing' in a directory to see whether it's a file or not (using the PHP built in function) and output what you need accordingly.

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
Code:
/* ######## Searchdir ########
What is it: It's a dirlisting script, based on Nicolas Merlet - admin(at)merletn.org example on [URL unfurl="true"]www.php.net,[/URL]
as improved by josh _at_ thenewfood _dot_ com.
It has been slightly modified by Olav Alexander Mjelde

How it works:
It quite simply gathers a dirlist script and returns an array with the dirlisting.
CAUTION: If only path is specified, it crawls unlimited sub-dirs!!
*/

function searchdir ( $path , $maxdepth = -1 , $mode = "FULL" , $d = 0, $exclude = "index.php" )
{
// $path : path to browse
// $maxdepth : how deep to browse (-1=unlimited)
// $mode : "FULL"|"DIRS"|"FILES"
// $d : must not be defined


   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 != $exclude)
           {
               $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 ) ;
}

you could improve this one, by making it accept an array of excludes and then check if current is in array.

Ps. this works (for what I made it for, many years ago).
Its part of an opensource unreal tournament log ban-search script I made for this dude.

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top