mhodgson Posted December 5, 2008 Share Posted December 5, 2008 I have the following code to list all the (sub)directories within a particular directory and put them in a drop list. $dir = "images/"; //You could add a $_get to change the directory $files = scandir($dir); //$files = asort($files); // Doesn't work echo '<SELECT name=category>'; foreach ($files as $key => $value) { //if (is_dir($entry) && ($entry!=".") && ($entry!="..")) // This doesn't work if (($value!=".") && ($value!="..")) // But this does {echo '<OPTION value='.$value.'> '.$value.'';} } echo '</select>'; It works well but will also show any files in the directory. I would also like to sort it alphabetically but can't seem to get it to work Any ideas ? Thanks Link to comment https://forums.phpfreaks.com/topic/135644-drop-list-of-file-directories/ Share on other sites More sharing options...
rhodesa Posted December 5, 2008 Share Posted December 5, 2008 $dir = "images/"; //You could add a $_get to change the directory if(!$dir = realpath($dir)) die("Folder does not exist"); $files = scandir($dir); sort($files); // should work now. the sort functions modify the variable and don't return anything echo '<SELECT name=category>'; foreach ($files as $value) { if($value{0} == '.') continue; //Skip anything that starts with a period if(!is_dir($dir.DIRECTORY_SEPARATOR.$value)) continue; //Skip if not a directory echo '<optionvalue='.$value.'> '.$value.'</option>'; } echo '</select>'; Link to comment https://forums.phpfreaks.com/topic/135644-drop-list-of-file-directories/#findComment-706693 Share on other sites More sharing options...
mhodgson Posted December 5, 2008 Author Share Posted December 5, 2008 Thanks, works great after I found the typo in the option value (needed a space). I have akso found this natcasesort() to sort irespective of the letter case in the file name Many thanks. Link to comment https://forums.phpfreaks.com/topic/135644-drop-list-of-file-directories/#findComment-706721 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.