Jump to content

paulonline2501

Recommended Posts

hi everyone, im trying to write a simple php script to list directories in a folder x. i cna get the code to give me a list of whats in the folder no probs:

<?php   if ($handle = opendir('../subdomains')) {    while (false !== ($entry = readdir($handle))) {	 if ($entry != "." && $entry != "..") {	  echo "<li><a href=\"../subdomains/$entry\">$entry</a></li>";	 }    }    closedir($handle);   }  ?>

...but this will list file names aswell. i know i can add names i dont want to the... if($entry != "") ...but i dont think thats very elegent.i was hoping there'ld be an inbuilt php function for listing directorys but there doesnt seem to be. what i was goin to do was try to check using isdir() function [unless theres something better i could use thats designed for to list directories.] i came up with something like this but it didnt work:

<?php   if ($handle = opendir('../subdomains')) {    while (false !== ($entry = readdir($handle))) {	   if ($entry != "." && $entry != "..") {		   if(false !== var_dump(isdir($entry))){					  echo "<li><a href=\"../subdomains/$entry\">$entry</a></li>";			}	  	   }    }    closedir($handle);   }  ?>

...any ideas where its going wrong. or more importantly is there a function that does this???? best wishes

Link to comment
Share on other sites

the function name is is_dir(). and you dony need to use var_dump()it does not return anything it is used for debug purpose.http://php.net/is_dirhttp://php.net/var_dump

Link to comment
Share on other sites

hi dsonesuk, thats great! works perfectly...

<?php   $path = "../subdomains/";   foreach(glob($path.'*', GLOB_ONLYDIR) as $dir){	 $dir = basename($dir);    echo '<li><a href="../subdomains/'.$dir.'">'.$dir.'</a></li>';   }  ?>

...and even better it matches the code of my file list script:

<?php	 foreach (glob('../test/*.php') as $file)	 {		 $file = basename($file);	   if ($file != 'index.php')	   {echo "<li><a href=\"$file\">$file</a></li>";}	 }    ?>

thanks for the help.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...