Jump to content

putting scandir result in a dropdown


billckr

Recommended Posts

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

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.

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.";
}
?>

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 :D

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 :D

 

I tried what I thought was this exact thing several times. Clearly I had something out of place.

 

Thanks BillBob!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.