dmirsch Posted September 20, 2011 Share Posted September 20, 2011 In the MySQL help forum someone asked how to list out all the files in a directory. Another person gave the following code: <?php $handle = opendir('PUT NAME OF YOUR DIR HERE'); while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { echo $file,'<br />'; } } closedir($handle); ?> I found this to be very helpful! However, is there a way to modify it so that if there are subdirectories, it will list those as hyperlinks so the code would run off of the subdirectory if you were to click that link? When I asked that question in the MySQL forum, I was told that it had nothing to do with PHP which I know. So that's why I am posting it here. Can anyone help? Quote Link to comment Share on other sites More sharing options...
xyph Posted September 20, 2011 Share Posted September 20, 2011 $path = '/path/to/your/folder/'; $handle = opendir($path); while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if( is_dir($path.$file) ) echo '<a href="/'.$file.'">'.$file.'</a>'; else echo $file; echo '<br>'; } } closedir($handle); Quote Link to comment Share on other sites More sharing options...
dmirsch Posted September 20, 2011 Author Share Posted September 20, 2011 So I used xyph's code and it is not reading anything as a directory. Here's how I modified the code: <?php $path = '../../images'; $handle = opendir($path); while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if( is_dir($path.$file) ) echo '<a href="'.$path.$file.'">'.$file.'</a> is a directory'; else echo $file.' is not a directory'; echo '<br>'; } } closedir($handle); ?> The sample page is here: http://acpacenterstage.com/centerstage/centertix-managers/reading-files-in-folder.php As you can see, nothing is reading as a directory even though all but the last two are directories. Any ideas? Quote Link to comment Share on other sites More sharing options...
xyph Posted September 20, 2011 Share Posted September 20, 2011 Try echo'ing $path.$file $path needs a trailing slash, like my example. Quote Link to comment Share on other sites More sharing options...
dmirsch Posted September 21, 2011 Author Share Posted September 21, 2011 Close, I had to add a forward slash to make it really go to the directory. echo '<a href="'.$path.$file.'/">'.$file.'</a> is a directory'; However, I would like the code to rerun with that new directory once the hyperlink is clicked, because now it tells me, Access Forbidden Access denied. Please click on the back button to return to the former page. since there is no real file name listed in the hyperlink. So somehow if I'd like the coding to keep cycling through until it see no more subdirectories. Is that possible? Quote Link to comment Share on other sites More sharing options...
jcbones Posted September 21, 2011 Share Posted September 21, 2011 From my library. Make sure you don't close the directory with a slash. ex. full/path/to/directory/ <-wrong full/path/to/directory <-right <?php function dirContents($dir) { if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if(in_array($file,array('.','..'))) { continue; } if(is_dir($dir.'/'.$file)) { $contents[$dir.'/'.$file] = dirContents($dir.'/'.$file); } else { $contents[] = $dir . '/' . $file; } } closedir($dh); } } return $contents; } function processArray($array) { if(is_array($array)) { echo '<ol>'; foreach($array as $key => $value) { if(is_int($key)) { echo '<li><a href="'.$value.'">'.str_replace('/','',strstr(strrchr($value,'/'),'.',true)).'</a></li>'; } else { echo '<li><span style="font-weight:bold">' . $key . '</span>'; processArray($value); echo '</li>'; } } echo '</ol>'; } } processArray(dirContents('PATH TO DIRECTORY')); ?> Quote Link to comment Share on other sites More sharing options...
dmirsch Posted September 21, 2011 Author Share Posted September 21, 2011 This is almost there. It gives me the following error though: Warning: Wrong parameter count for strstr() in /data/23/2/100/53/2263053/user/2487001/htdocs/centerstage/centertix-managers/reading-files-in-folder.php on line 42 Here's the code that goes on line 42: echo '<li><a href="'.$value.'">'.str_replace('/','',strstr(strrchr($value,'/'),'.',true)).'</a></li>'; Quote Link to comment Share on other sites More sharing options...
jcbones Posted September 21, 2011 Share Posted September 21, 2011 Yes, the optional parameter to strstr() is available only on PHP 5.3.0 and later versions. Hold on a sec, and I will get a work around to that. Quote Link to comment Share on other sites More sharing options...
jcbones Posted September 21, 2011 Share Posted September 21, 2011 Replace that line with these two lines. $name = strrchr($value,'/'); echo '<li><a href="'.$value.'">'.substr($name,1).'</a></li>'; Quote Link to comment Share on other sites More sharing options...
dmirsch Posted September 21, 2011 Author Share Posted September 21, 2011 Thanks jcbones! This works. Last thing though, there are a bunch of .LCK files on the server that I do not want to show up in the list. If there an easy way to edit the coding to make those a no-show? Here's the code once again: <?php function dirContents($dir) { if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if(in_array($file,array('.','..'))) { continue; } if(is_dir($dir.'/'.$file)) { $contents[$dir.'/'.$file] = dirContents($dir.'/'.$file); } else { $contents[] = $dir . '/' . $file; } } closedir($dh); } } return $contents; } function processArray($array) { if(is_array($array)) { echo '<ol>'; foreach($array as $key => $value) { if(is_int($key)) { $name = strrchr($value,'/'); echo '<li><a href="'.$value.'">'.substr($name,1).'</a></li>'; } else { echo '<li><span style="font-weight:bold">' . $key . '</span>'; processArray($value); echo '</li>'; } } echo '</ol>'; } } processArray(dirContents('../../images')); ?> Quote Link to comment Share on other sites More sharing options...
jcbones Posted September 22, 2011 Share Posted September 22, 2011 Change: if(in_array($file,array('.','..'))) { continue; } To: if(in_array($file,array('.','..')) || strrchr($file,'.') == '.LCK') { continue; } This is un-tested, so let me know how it goes. Quote Link to comment Share on other sites More sharing options...
dmirsch Posted September 22, 2011 Author Share Posted September 22, 2011 Yeah! This worked. Thanks so much! Quote Link to comment Share on other sites More sharing options...
FG_immo Posted December 12, 2016 Share Posted December 12, 2016 Hi everyone,I've stumbled across this script a while ago (and it's working fine, thanks btw)However, I noticed that the order of the files listed seems to be random. How can I change the script to make it list the files alphabetically? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted December 12, 2016 Share Posted December 12, 2016 This thread is from 2011. Most of the people aren't even active any longer, and they won't come back just to give you personal support. Create your own threads with your own code. The glob() function will help you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.