Hello,
I want to read all the files in a folder & display them in a drop down menu. I'm using the code below & it works fine. I now want the file names to be sorted ascending before it is displayed in the drop down menu. How do I do that? Thank you very much for your help.

<select size="1" name="project">
<?php
if ($handle = opendir('/path/to/my/files/here/')) {
while (false !== ($file = readdir($handle))) {
 if ($file != "." && $file != "..") {
 echo "$file\n";
 echo '<option value="$file\n"</option>';
 }
}
closedir($handle);
}
?>
</select>

    load everything into an array, then use natcasesort(), then loop through the array to create the options.

      <select size="1" name="project">
      <?php
      // Initialize the array that will hold the
      // file names
      $files = array();
      
      if ($handle = @opendir('/path/to/my/files/here/')) {
      while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != "..") {
      
      // Add the file (if it is a file) to the array
      $files[] = $file;
      }
      }
      closedir($handle);
      
      // If we have items in the array, sort the files naturally
      if(count($files) > 0) natcasesort($files);
      
      // Loop through the sorted array and
      // print out the files
      foreach($files as $output){
      echo "$output\n";
      echo '<option value="$output\n"</option>';
      }
      }
      ?>
      </select>
      

      I got bored so I wrote the code for you 😉

      [edit] I made an edit for error checking.

        Very helpful of you, rkolbe. I truly appreciate your effort. Have a nice day, man :-)

          Write a Reply...