Brian W Posted September 29, 2008 Share Posted September 29, 2008 I'm just messin around with the dir() functions to learn how they work and how they could be helpful and encountered a problem. Not all of the directories are being caught by if(is_dir($filename) == TRUE)... so some of them are being treated like files. Here is my code. <?php clearstatcache(); $dir = "EPM/"; if(isset($_GET['dir'])) { $dir = $_GET['dir']; } $handle = opendir($dir); if($handle == FALSE) { die("Error opening dir."); } echo "Files in: " . $dir . "<br>"; while(false !== ($filename = readdir($handle))) { if($filename == "." or $filename == "..") { echo $filename.'<br>';} else { if(is_dir($filename) == TRUE) { echo '<a href="?dir='.$dir.$filename.'/"> -> '.$filename.'</a><br>'; } else { echo '<a href="'.$dir.$filename.'">'.$filename.'</a><br>'; }} } closedir($handle); ?> Link to comment https://forums.phpfreaks.com/topic/126351-is_dir-hiccup/ Share on other sites More sharing options...
DarkWater Posted September 29, 2008 Share Posted September 29, 2008 Check out the glob function and this could be written in like, 6 lines. Link to comment https://forums.phpfreaks.com/topic/126351-is_dir-hiccup/#findComment-653409 Share on other sites More sharing options...
Brian W Posted September 29, 2008 Author Share Posted September 29, 2008 Oh, thank you... I didn't find glob() withing the manual when I was searching the directory stuff. I still wouldn't mind knowing why what I was doing was skipping over some directories. You know, for learning purposes and quality assurance. Link to comment https://forums.phpfreaks.com/topic/126351-is_dir-hiccup/#findComment-653431 Share on other sites More sharing options...
Brian W Posted September 29, 2008 Author Share Posted September 29, 2008 Still wondering why my attempt #1 was skipping over some directories, or is that a mystery better off forgotten? :-\ I created this file-tree script for fun, its very interesting to do it in your the root directory. File 1 = filelist.php <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Files</title> <style type="text/css"> <!-- body li { list-style-type: square; list-style-position: outside; text-indent: 10px; } --> </style> </head> <body><div id="files"> <?php $dir = 'EPM/'; if(isset($_GET['dir'])) { $dir = $_GET['dir'] ; } ?> <h3>Files in: <?php echo $dir; ?></h3> <?php foreach (glob($dir."*", GLOB_MARK) as $filename) { if(is_dir($filename)) { $tdir = $filename; echo '<a href="?dir='.$filename.'">>'.$filename.'</a><br>'; include('filelist2.php');} else { echo '<li><a href="'.$filename.'">'.$filename.'</a></li>'; }}?> </div> </body> File 2 = filelist2.php <ul><?php foreach (glob($tdir."*", GLOB_MARK) as $filename) { if(is_dir($filename)) { $tdir = $filename; echo '<a href="?dir='.$filename.'">'.$filename.'</a><br>'; include('filelist2.php');} else { echo '<li><a href="'.$filename.'">'.$filename.'</a><br></li>'; }}?>></ul> to see the root, simply have the url look like this .../filelist.php?dir=/ Also, you can change the defult folder by changing $dir = I hit the stop button after about 30 seconds, little worried about what might happen. Link to comment https://forums.phpfreaks.com/topic/126351-is_dir-hiccup/#findComment-653452 Share on other sites More sharing options...
Brian W Posted October 1, 2008 Author Share Posted October 1, 2008 WOOPS> SORRY< miss-fire Link to comment https://forums.phpfreaks.com/topic/126351-is_dir-hiccup/#findComment-654470 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.