Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8986417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:32:41+00:00 2026-06-15T21:32:41+00:00

I am trying to populate an HTML form’s drop down list with the contents

  • 0

I am trying to populate an HTML form’s drop down list with the contents of the current directory. I attempted to use PHP’s scandir() function to output these files as an array and then feed them into the option’s value tag within the HTML form.

I looked at various solutions available on SO and outside, but none seemed to work for me.

This is how my code looks like right now:

<form action="" >
        <input type="submit" class="Links" value="Select Input">
        <select >               
            <?php 
                $dir="./inputfiles/"; 
                $filenames = scandir($dir); 
                foreach($filenames as $file){
                    echo '<option value="' . $filenames . '">' . $filenames . '</option>';
                    }
            ?>                      
        </select> 
</form> 

For now I’m getting absolutely no options in the drop down menu.

I am very new to PHP and would appreciate your feedback as to how to make this work.


Further Changes:

I tried the solutions given below, and none seemed to work. I changed the code to check whether the PHP script is able to output any variable or not in the html forms list.

<form action="" >
        <input type="submit" class="Links" value="Select Input">
        <select >               
            <?php 
                  $someval = "Video";
                  echo '<option value="value"> ' . $someval .' </option>';
            ?>                      
        </select> 
</form>

It displays ‘ . $someval .’ instead of Video in the menu bar

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:32:43+00:00Added an answer on June 15, 2026 at 9:32 pm

    A couple of concerns with your script.

    First, you need to output the correct variable within the options:

    echo '<option value="' . $file . '">' . $file . '</option>';
    

    Second, you will likely need to add detection for . and .. in your loop, as you will likely not want to output them. You might also want to exclude directories as well.

    Third, you should probably make it clear exactly what directory you are trying to scan. Try using a full file path. If you need it relative to the currently executing file’s directory you can do something like:

    $dir= __DIR__ . DIRECTORY_SEPARATOR . 'inputfiles'; // no need for trailing slash
    

    Fourth, you might want to consider working with something like DirectoryIterator to give you some more convenient methods to do what you are looking to do (i.e. verifying it’s an actual file before listing it). So something like this:

    $dir= __DIR__ . DIRECTORY_SEPARATOR . 'inputfiles';
    $iterator = new DirectoryIterator($dir);
    foreach ($iterator as $fileinfo) {
        if($fileinfo->isFile()) {
            echo '<option value="' . $fileinfo->getFilename() . '">' . $fileinfo->getFilename() . '</option>';
        }
    }
    

    In this trivial example, it you won’t necessarily gain a lot (in terms of saving extra code) by using DirectoryIterator, but I certainly like to point out it’s usage when I can, because if you needed to start doing things like showing file size, file permissions, file modification times, etc. It is much easier to do it using the DirectoryIterator class (or RecursiveDirectoryIterator class if you need to recursive) than using traditional PHP’s file system functions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to populate a drop down list when a specific list item
I'm trying to use a php function within a template html form that is
I am just trying to populate a html.dropdown list using mvc2 in VS2008. But
I am trying to populate a select list with AJAX as follows: HTML: <table>
Trying to achieve the drop down effect I have on my form here once
Simple problem , I am trying to populate html form check-boxes dynamically, but when
I am trying to run BeanUtilsBean.getInstance().populate(...) but on the HTML form, there is a
I am trying to dynamically build a drop down menu using PHP. The idea
I trying to populate the dropdown list when page was loaded.But it is not
I am trying to populate a List<Image> by reading the name of the each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.