Jump to content

Drop list of file directories


mhodgson

Recommended Posts

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

$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>';

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.