aussiexile Posted June 25, 2008 Share Posted June 25, 2008 ok im a bit stumped at this and its probably such an easy thing to fix but im trying to read through a directory and print out a list of the folders in the dir i got this code off the net (cant remember where) <?php $list_ignore = array ('.','..'); $handle=opendir("."); while ($file = readdir($handle)) { if (is_dir($file) && !in_array($file,$list_ignore)) { ?> <div class="cssnav"><a href="<?php echo $file?>"><img src="img/dossier.gif"/><span><?php echo $file?></span></a></div> <?php } } closedir($handle); ?> and that works fine but i want it to read through a sub dir eg: <?php $list_ignore = array ('.','..'); $handle=opendir("templates"); while ($file = readdir($handle)) { if (is_dir($file) && !in_array($file,$list_ignore)) { ?> <div class="cssnav"><a href="<?php echo $file?>"><img src="img/dossier.gif"/><span><?php echo $file?></span></a></div> <?php } } closedir($handle); ?> and there are folders in the dir but it wont show them, if i remove the is_dir($file) it prints them out but also any other file, yet if i go <?php $dir = 'templates'; if(is_dir($dir.'42')){ echo 'True'; } else { echo 'False' } ?> its shows up true......can anyone shed any light on this?? thanx Link to comment https://forums.phpfreaks.com/topic/111829-trouble-reading-a-directory/ Share on other sites More sharing options...
rhodesa Posted June 25, 2008 Share Posted June 25, 2008 try this out: <?php $list_ignore = array ('.','..'); $dir = 'templates'; $handle=opendir($dir); while ($file = readdir($handle)) { //$file is just the filename $path = $dir.'/'.$file; //$path is now the full path including the directory name if (!in_array($file,$list_ignore)) { echo '<div class="cssnav"><a href="'.$path.'"><img src="img/dossier.gif"/><span>'.$file.'</span></a></div>'; } } closedir($handle); ?> Link to comment https://forums.phpfreaks.com/topic/111829-trouble-reading-a-directory/#findComment-574070 Share on other sites More sharing options...
aussiexile Posted June 25, 2008 Author Share Posted June 25, 2008 i want to ensure that only directories are displayed. i guess i could use is_dir($path)... will try it out and post my results Link to comment https://forums.phpfreaks.com/topic/111829-trouble-reading-a-directory/#findComment-574100 Share on other sites More sharing options...
aussiexile Posted June 25, 2008 Author Share Posted June 25, 2008 ok got it wokring this is the final code <?php $list_ignore = array ('.','..'); $dir = 'templates'; $handle=opendir($dir); while ($file = readdir($handle)) { //$file is just the filename $path = $dir.'/'.$file; //$path is now the full path including the directory name if (is_dir($path) && !in_array($file,$list_ignore)) { echo '<div class="cssnav"><a href="'.$path.'"><img src="img/dossier.gif"/><span>'.$file.'</span></a></div>'; } } closedir($handle); ?> Thanks for the help rhodesa Link to comment https://forums.phpfreaks.com/topic/111829-trouble-reading-a-directory/#findComment-574102 Share on other sites More sharing options...
rhodesa Posted June 25, 2008 Share Posted June 25, 2008 yeah, missed that from your original post Link to comment https://forums.phpfreaks.com/topic/111829-trouble-reading-a-directory/#findComment-574111 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.