Forum Moderators: coopster

Message Too Old, No Replies

Listing only FOLDERS and excluding certain folders

need help excluding regular files and specified directories

         

cookiemonster

9:13 pm on May 31, 2010 (gmt 0)

10+ Year Member



I recently found this code on a website. It lists all folders and files in a certain directory and makes links out of them.
My question is, can the script be used to only list FOLDERS instead of files AND folders?
Also, can it be modified to exlude specified folders (i.e., exclude testfolder/)?

Thanks in advance!


<?php
function listFolders(){
$path = glob("*"); //the double \\ is not a typo you
//have to escape it
//if the path cannot be found
if(!($path)){
echo("The path cannot be found");
return;
}//if

//the path is valid
else{
foreach($path as $i){

echo("<a href='$i'>$i</a><br>");

}//foreach
}//else
}//populateDropdown
?>
<?php echo listFolders(); ?>

optik

2:13 pm on Jun 1, 2010 (gmt 0)

10+ Year Member



From the PHP manual in looks like $path=glob("*",GLOB_ONLYDIR ); would return only directories

I think you would have to add an if statement in the for each loop to exclude certain folders.

cookiemonster

9:28 pm on Jun 2, 2010 (gmt 0)

10+ Year Member



Thanks, optik ... worked perfectly.

cookiemonster

9:31 pm on Jun 2, 2010 (gmt 0)

10+ Year Member



Actually, could you give me an example of an if statement that I would use to exclude specific folders?
I'm new to PHP, so every bit of help I can get is great.

Thanks again!

lorum

9:22 am on Jun 9, 2010 (gmt 0)

10+ Year Member




foreach($path as $i){

if($i != "excluded folder name" || $i != "Another excluded folder name")
{
echo("<a href='$i'>$i</a><br>");
}

}