tjhilder Posted July 15, 2006 Share Posted July 15, 2006 Hi,I have some code that lists everything in the directory it's pointing to, but I want to only list directories, I've had a good look around php.net but can't seem to find what i'm looking for so I was hoping someone here could give me a hand.here's my code:[code]echo "<select name=\"album_name\">\n";echo "\t<option selected=\"selected\" value=\"\">Select an Album</option>\n";if ($handle = opendir("../../gallery/albums")){ while ($file = readdir($handle)) { if (strlen($file) >3) { $files[] = $file; } } closedir($handle); sort($files); foreach ($files as $file) { echo "\t<option value=\"{$file}\">$file</option>\n"; }}echo "</select>\n";[/code]--TJ Link to comment https://forums.phpfreaks.com/topic/14690-listing-directories-only/ Share on other sites More sharing options...
AndyB Posted July 15, 2006 Share Posted July 15, 2006 Take a look at the code from renich at woralelandia dot com on the manual's http://ca.php.net/manual/en/function.is-dir.php page. I *think* that's your solution. Link to comment https://forums.phpfreaks.com/topic/14690-listing-directories-only/#findComment-58604 Share on other sites More sharing options...
ShogunWarrior Posted July 15, 2006 Share Posted July 15, 2006 [code]if ($handle = opendir("../../gallery/albums")){ while ($file = readdir($handle)) { if (strlen($file) >3 && is_dir($file)) { $files[] = $file; } } closedir($handle); sort($files); foreach ($files as $file) { echo "\t<option value=\"{$file}\">$file</option>\n"; }}echo "</select>\n";[/code]Should do the trick.I just added a check for [b]is_dir[/b] which checks if $file is a directory. Link to comment https://forums.phpfreaks.com/topic/14690-listing-directories-only/#findComment-58605 Share on other sites More sharing options...
tjhilder Posted July 15, 2006 Author Share Posted July 15, 2006 Hi,[b]AndyB[/b], thanks for the link but I've checked out that one a few times and i've tried different things but can't get it to work.[b]ShogunWarrior[/b], that code doesn't work :( using that code I get these errors:[code]<select name="album_name"> <option selected="selected" value="">Select an Album</option><br /><b>Warning</b>: sort() expects parameter 1 to be array, null given in <b>/home/tjhilder/public_html/four/admin/one.php</b> on line <b>15</b><br /><br /><b>Warning</b>: Invalid argument supplied for foreach() in <b>/home/tjhilder/public_html/four/admin/one.php</b> on line <b>16</b><br /></select>[/code] Link to comment https://forums.phpfreaks.com/topic/14690-listing-directories-only/#findComment-58664 Share on other sites More sharing options...
ShogunWarrior Posted July 15, 2006 Share Posted July 15, 2006 [b]readdir[/b] only returns the filename, so when you check [b]is_dir[/b] it's looking for the file in the current directory, this should work:[code]$dirname= "../../gallery/albums/";if ($handle = opendir($dirname)){ while ($file = readdir($handle)) { if (strlen($file) >3 && is_dir($dirname.$file)) { $files[] = $file; } } closedir($handle); sort($files); foreach ($files as $file) { echo "\t<option value=\"{$file}\">$file</option>\n"; }}echo "</select>\n";[/code] Link to comment https://forums.phpfreaks.com/topic/14690-listing-directories-only/#findComment-58702 Share on other sites More sharing options...
tjhilder Posted July 16, 2006 Author Share Posted July 16, 2006 thanks so much, it works great! ;D Link to comment https://forums.phpfreaks.com/topic/14690-listing-directories-only/#findComment-59082 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.