billckr Posted July 8, 2010 Share Posted July 8, 2010 HI, I am trying to use scandir and place the results of the array in a drop down. Firstly I would like to exclude anything but directories if possible. There will be several .php and .html files in the directory that I would like to leave out of the results as well as the "." and "..". Maybe scandir is not the best options with these requirements? The code below prints out the results using echo or print just find, but it's the whole array and not just the names of the folders. Array ( [0] => . [1] => .. [2] => 2006-12 2006-12 is the name of one of the folders I want. How can I get the results of ; $dir = '/some/dir'; $files1 = scandir($dir); into a drop down with just the folder names? Thank you for your help. Link to comment https://forums.phpfreaks.com/topic/207187-putting-scandir-result-in-a-dropdown/ Share on other sites More sharing options...
JasonLewis Posted July 9, 2010 Share Posted July 9, 2010 Use a combination of glob (include a flag, GLOB_ONLYDIR would suffice), and a foreach loop. This is something basic: foreach(glob("*", GLOB_ONLYDIR) as $dir){ echo $dir . "<br />"; } You should be able to figure it out with that. Good luck. Link to comment https://forums.phpfreaks.com/topic/207187-putting-scandir-result-in-a-dropdown/#findComment-1083612 Share on other sites More sharing options...
Wolphie Posted July 9, 2010 Share Posted July 9, 2010 I've used an array to define which files you want to exclude from the output, it makes adding and removing exclusion easy. This way you can also easily manipulate the output style. <?php $dir = 'path/to/dir'; // List of file names to exclude from output // NOTE: This is case-sensitive $exclude = array('somefile.php', 'somedir'); // Check to see if $dir is a valid directory if (is_dir($dir)) { $contents = scandir($dir); echo '<ul>'; foreach($contents as $file) { // This will exclude all filenames contained within the $exclude array // as well as hidden files that begin with '.' if (!in_array($file, $exclude) && substr($file, 0, 1) != '.') { echo '<li>'. $file .'</li>'; } } echo '</ul>'; } else { echo "The directory <strong>". $dir ."</strong> doesn't exist."; } ?> Link to comment https://forums.phpfreaks.com/topic/207187-putting-scandir-result-in-a-dropdown/#findComment-1083621 Share on other sites More sharing options...
billckr Posted July 9, 2010 Author Share Posted July 9, 2010 Thank you both. Sorry for the delay in replying i've been on Plane all day and just landed. I'll try these out this evening and report back the results. Link to comment https://forums.phpfreaks.com/topic/207187-putting-scandir-result-in-a-dropdown/#findComment-1083775 Share on other sites More sharing options...
billckr Posted July 10, 2010 Author Share Posted July 10, 2010 Both of these work out of the box but I'm having a devil of a time getting the results into the drop down menu. It's because they are in an array I guess. Might I ask for a small example that I could work with? Thank you very much. Link to comment https://forums.phpfreaks.com/topic/207187-putting-scandir-result-in-a-dropdown/#findComment-1083935 Share on other sites More sharing options...
BillyBoB Posted July 10, 2010 Share Posted July 10, 2010 Try just changing the html up. <?php $dir = 'path/to/dir'; // List of file names to exclude from output // NOTE: This is case-sensitive $exclude = array('somefile.php', 'somedir'); // Check to see if $dir is a valid directory if (is_dir($dir)) { $contents = scandir($dir); echo '<select>'; foreach($contents as $file) { // This will exclude all filenames contained within the $exclude array // as well as hidden files that begin with '.' if (!in_array($file, $exclude) && substr($file, 0, 1) != '.') { echo '<option>'. $file .'</option>'; } } echo '</select>'; } else { echo "The directory <strong>". $dir ."</strong> doesn't exist."; } ?> There ya go Link to comment https://forums.phpfreaks.com/topic/207187-putting-scandir-result-in-a-dropdown/#findComment-1083940 Share on other sites More sharing options...
billckr Posted July 10, 2010 Author Share Posted July 10, 2010 Try just changing the html up. <?php $dir = 'path/to/dir'; // List of file names to exclude from output // NOTE: This is case-sensitive $exclude = array('somefile.php', 'somedir'); // Check to see if $dir is a valid directory if (is_dir($dir)) { $contents = scandir($dir); echo '<select>'; foreach($contents as $file) { // This will exclude all filenames contained within the $exclude array // as well as hidden files that begin with '.' if (!in_array($file, $exclude) && substr($file, 0, 1) != '.') { echo '<option>'. $file .'</option>'; } } echo '</select>'; } else { echo "The directory <strong>". $dir ."</strong> doesn't exist."; } ?> There ya go I tried what I thought was this exact thing several times. Clearly I had something out of place. Thanks BillBob! Link to comment https://forums.phpfreaks.com/topic/207187-putting-scandir-result-in-a-dropdown/#findComment-1083982 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.