spikypunker Posted April 13, 2010 Share Posted April 13, 2010 Hi guys! I need some help! All i need to do is list the contents of a directory into a dropdown, with the filenames as the list values. I've found a script which does this, <?php // Set the path of the dir you want displayed... $path="../emails/ama/images/headers/"; $handle=opendir($path); while ($file=readdir($handle)) { echo "\t<option value='#/ama/images/headers/".$file."'>".$file."</option>\n"; } ?> Now this isnt doing a coupla important things, it's not sorting them, and it's including "." and ".." I've found a couple of much better scripts which put the results into an array and also excluse the "." etc BUT i dont know how to then perform a loop to populate the drop down from the array? It would be awesome if someone could first show me how to read into the array, and then loop the array into a dropdown? It's cool doing it that way cause i can then learn something rather than just finding script and slapping it in. Cheers for any help, Peace, Chris Quote Link to comment https://forums.phpfreaks.com/topic/198403-dir-contents-into-a-dropdown/ Share on other sites More sharing options...
JonnoTheDev Posted April 13, 2010 Share Posted April 13, 2010 <?php $path = "../emails/ama/images/headers/"; $handle = opendir($path); $files = array(); // build an array of files while($file = readdir($handle)) { if($file != "." && $file != "..") { $files[] = $file; } } // sort the array sort($files); // loop through and populate the select list foreach($files as $file) { echo "\t<option value='#/ama/images/headers/".$file."'>".$file."</option>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/198403-dir-contents-into-a-dropdown/#findComment-1041068 Share on other sites More sharing options...
spikypunker Posted April 14, 2010 Author Share Posted April 14, 2010 wow thats awesome! Cheers it's works 99%, exactly what i needed however, check out this slightly wierd result... <option value='BirdsEyeScenic.jpg'>BirdsEyeScenic.jpg</option> <option value='BirdsEyeScenic2.jpg'>BirdsEyeScenic2.jpg</option> <option value='BoutiqueAndShips.jpg'>BoutiqueAndShips.jpg</option> <option value='Bridges.jpg'>Bridges.jpg</option> <option value='ColourfulTown.jpg'>ColourfulTown.jpg</option> <option value='headers/DayScenic.jpg'>DayScenic.jpg</option> <option value='headers/Dining.jpg'>Dining.jpg</option> <option value='headers/DoubleBridge.jpg'>DoubleBridge.jpg</option> <option value='Festival.jpg'>Festival.jpg</option> <option value='InteriorStyle.jpg'>InteriorStyle.jpg</option> <option value='headers/InteriorStyle2.jpg'>InteriorStyle2.jpg</option> <option value='Lounge.jpg'>Lounge.jpg</option> <option value='NightScenic.jpg'>NightScenic.jpg</option> <option value='OldCastleOverlookingRiver.jpg'>OldCastleOverlookingRiver.jpg</option> <option value='OnboardScenic.jpg'>OnboardScenic.jpg</option> <option value='PoolDeck.jpg'>PoolDeck.jpg</option> <option value='RussianShip.jpg'>RussianShip.jpg</option> <option value='Scenic2.jpg'>Scenic2.jpg</option> <option value='Scenic3.jpg'>Scenic3.jpg</option> <option value='ScenicWithBridge.jpg'>ScenicWithBridge.jpg</option> <option value='ScenicWithCastle.jpg'>ScenicWithCastle.jpg</option> <option value='ShipOnWaterLarge.jpg'>ShipOnWaterLarge.jpg</option> <option value='TowerScenic.jpg'>TowerScenic.jpg</option> <option value='Tulips.jpg'>Tulips.jpg</option> <option value='Twilight.jpg'>Twilight.jpg</option> <option value='ama1.jpg'>ama1.jpg</option> <option value='cabin.jpg'>cabin.jpg</option> <option value='crew.jpg'>crew.jpg</option> <option value='crew2.jpg'>crew2.jpg</option> The alphabet has started again, i've noticed that they seem to have been seperated into uppper and lower case? peace Quote Link to comment https://forums.phpfreaks.com/topic/198403-dir-contents-into-a-dropdown/#findComment-1041482 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.