Forum Moderators: coopster

Message Too Old, No Replies

list directory and files

         

skoff

8:13 pm on Jun 22, 2011 (gmt 0)

10+ Year Member



Hi guys,
i know that this exist and i searched for it but seemed I cant get it to work. Want i want to do exactly is that I need a script to get a list of all folders in one directory. And I need another script to get a list of all files in a folders with only a certain extension.

Thanks for your help! :)

penders

8:40 pm on Jun 22, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You could check out PHP's DirectoryIterator [uk3.php.net] class - the first example lists all the files, but can easily be adapted for directories (call isDir() instead of isFile()) and then to filter files by extension...

Post back if you get stuck.

rocknbil

4:30 pm on Jun 23, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's a simple starting point. Not debugged but should work.


$dir = $_SERVER['DOCUMENT_ROOT'] . "/myfolder";
$ext = 'png|jpe*g|gif'; // match jpg or jpeg
if ($handle = opendir($dir)) {
echo "<ul>";
while (false !== ($file = readdir($handle))) {
if (is_dir("$dir/$file")) {
echo "<li>Directory: $dir/$file</li>";
}
else if (is_file("$dir/$file") and preg_match('/^.*\.$ext$/i',$file)) {
echo "<li>File: $dir/$file</li>";
}
}
closedir($handle);
echo "</ul>";
}
else { echo "<p>$dir has no files that match.</p>"; }