Page 1 of 1

Listing all files in a directory

Posted: Tue Sep 12, 2006 1:00 pm
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

Posted: Tue Sep 12, 2006 1:30 pm
by RobertGonzalez
The PHP filesystem functions: file() fopen() fread() fwrite() fclose().

Posted: Tue Sep 12, 2006 1:32 pm
by wtf

Posted: Tue Sep 12, 2006 1:36 pm
by thallish
thx.. just what I need;-)

Posted: Tue Sep 12, 2006 1:42 pm
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);
}

glob()

Posted: Tue Sep 12, 2006 1:52 pm
by akimm
look up glob

basically

Code: Select all

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