Listing all files in a directory

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
thallish
Forum Commoner
Posts: 60
Joined: Wed Mar 02, 2005 11:38 am
Location: Aalborg, Denmark

Listing all files in a directory

Post by thallish »

Hi

Does anyone know how/or what to use, to get all files in a directory listed.
What I'm looking for is just some accesstos which files are in the directory specified, the rest I can do on my own :lol:

I have been looking all over for this functionality, and I hope it can be one from a php point a view,
so that I dont have to write en extern C/C++ script.

Edit: yeah I can also do it by manipulating database data, but I 'll leave that as a last resort
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

The PHP filesystem functions: file() fopen() fread() fwrite() fclose().
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

thallish
Forum Commoner
Posts: 60
Joined: Wed Mar 02, 2005 11:38 am
Location: Aalborg, Denmark

Post by thallish »

thx.. just what I need;-)
thallish
Forum Commoner
Posts: 60
Joined: Wed Mar 02, 2005 11:38 am
Location: Aalborg, Denmark

Post by thallish »

Just to share what I just did:

Listing all the files in the current directory:

Code: Select all

if ($handle = opendir('.'))
{
	echo "<label class=\"label\">Filer:</label> <br />";

	// loop the directory
	while (false !== ($file = readdir($handle)))
	{
		// if we are looking at a file and it is 
		// not a part of PIP
		if (is_file($file) and 
 		    (strcmp($file,'index.php') and 
		     strcmp($file,'style_explorer.css') and 
		     strcmp($file,'style_mozilla.css') and 
		     strcmp($file,'style.css')) != 0 )
		  {
			echo '<p><a href="'. $file . '">'.$file.'</a></p>';
		  }	
	}

	closedir($handle);
}
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

glob()

Post by akimm »

look up glob

basically

Code: Select all

<?php
foreach (glob("sample_dir/sample_dir/*.php .html .htm") as $directory) {
   echo "$directory  . "\n";
}
?>
Post Reply