Jump to content

trouble reading a directory


aussiexile

Recommended Posts

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

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);
?>

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

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.