Tech-Evangelist

Technical Articles, Musings and Opinions from Tech-Evangelist

  • Home
  • About
  • Guidelines
Previous article: Digital Television Reception Problems After DTV Day
Next article: Using IP Addresses to Verify the Location of a Buyer

Using PHP to Display Directory Contents With a Select List

February 28, 2009 By Doogie - Copyright - All Rights Reserved

Sometimes there is a need to display the list of files in the directory and allow a user to select a file name from the list. We run into this most often when a user must be able to select an image or a document to be downloaded or displayed after being selected. Here is a very simple PHP script that will display the content of a web site directory using an HTML select list.

First of all, the script will need to be embedded in an HTML form in a script that will run PHP. It would take the place of a standard hard-coded drop-down select list or an input box. The difference is that this script will display the file names from a server directory that you designate. You will need to embed the script in a form so that you can pass the selected value to another script. This will not work in a standard script using the .htm or .html extension, unless your server is configured to run these script through the PHP parser.

This is the basic form of the select list script. As you can see, it is actually pretty simple and should be easy to implement if you have basic PHP skills. For the example we will display a list of image file names found in a directory. We are using PHP’s dir function to read the directory.

<select name="ImageFile">
<option value="">- Select Image -
<?php 
$dirPath = dir('/images');
while (($file = $dirPath->read()) !== false)
{
   echo "<option value=\"" . trim($file) . "\">" . $file . "\n";
}
$dirPath->close();
?>
</select>

The important part to make this script display the file names from the correct directory is to make sure that the path to the directory is correct. The path is set in the statement $dirPath = dir(‘/images’);. This path will work for a script in the root directory that is calling the /images/ directory that is one level down from the root. The path is a relative path, which means the path must be set with respect to the location of the script in a web site.

Here is what we have thus far:

The obvious problem with this directory select list is that the file names are not sorted. Unfortunately, the PHP dir directory function returns a list of file names that appears to be random. While that is not a significant issue if you only want to display 3 or 4 file names, it can be a problem if the directory contains a larger number of files. Ideally, you would want the list to be sorted alphanumerically.

Improving the Select List by Sorting the Directory Contents

For unknown reasons the PHP development team did not include a method to sort the results. However, there is a fairly simple modification that does accomplish this. We are going to read the images directory and pass the results to an array, sort the array, and use the array to form the option statements in the select box.

<select name="ImageFile">
<option value="">- Select Image -
<?php 
$dirPath = dir('/images');
$imgArray = array();
while (($file = $dirPath->read()) !== false)
{
   $imgArray[ ] = trim($file);
}
$dirPath->close();
sort($imgArray);
$c = count($imgArray);
for($i=0; $i<$c; $i++)
{
    echo "<option value=\"" . $imgArray[$i] . "\">" . $imgArray[$i] . "\n";
}
?>
</select>

The select list is now sorted as follows:

Filtering the Select List

If you have a directory with a mix of different file types, you probably do not want your list to be cluttered with unrelated files. You can easily filter the list displayed using the HTML select drop-down by adding a conditional statement

<select name="ImageFile">
<option value="">- Select Image -
<?php 
$dirPath = dir('/images');
$imgArray = array();
while (($file = $dirPath->read()) !== false)
{
  if ((substr($file, -3)=="gif") || (substr($file, -3)=="jpg") || (substr($file, -3)=="png"))
  {
     $imgArray[ ] = trim($file);
  }
}
$dirPath->close();
sort($imgArray);
$c = count($imgArray);
for($i=0; $i<$c; $i++)
{
    echo "<option value=\"" . $imgArray[$i] . "\">" . $imgArray[$i] . "\n";
}
?>
</select>

Notice the simple conditional statement addition that only allows image file types with an extension of gif, jpg or png to be displayed.

Filed Under: PHP Tutorials, Web Site Development

Comments

  1. Mark Kujoy says

    May 24, 2009 at 9:37 am

    Hiya.

    Thank you very much for this excellent tutorial. It was exactly what i was looking for, and very easy to implement.

    Mark

  2. Chris says

    November 20, 2009 at 12:26 pm

    This is great, but I get:

    - Select File -
    .
    ..
    1st Year Timeline.doc
    2009AreaCoordinator.pdf
    2009ChurchRelationsCoordinator.pdf

    Not sure why I get the dot values

  3. Aaxklonp says

    February 20, 2010 at 1:17 pm

    Does anyone know how to fix the ‘dot problem’? I like the script but that..

  4. Doogie says

    February 21, 2010 at 10:03 pm

    The dots are directories. You should not be seeing the dots if you use the filtered version of the script because that only displays image files, or whichever file extensions you designate.

  5. Internetguy says

    February 15, 2012 at 8:45 am

    Jut thank you for sharing this great solution, and yes, the “filtered version” definitely solves the dots problem.

    Thanks again.

  6. Achmed says

    August 20, 2012 at 4:47 am

    $opendir = “purchaseOrders/”;

    if ($handle = opendir($opendir)) {
    while (false !== ($file = readdir($handle))) {
    //first see if this file is required in the listing
    if ($file == “.” || $file == “..”) continue;
    echo “” . $file;

    }
    closedir($handle);
    }

Categories

  • Affiliate Marketing
  • CSS Tutorials
  • FileZilla Tutorials
  • Home Theater
  • Internet Marketing
  • Internet Technology
  • Kindle Tips
  • MySQL Tutorials
  • Online Auction Tips
  • Paint Shop Pro Tutorials
  • PHP Tutorials
  • Tech News
  • Thunderbird Tutorials
  • Video Production
  • Web Site Development
  • WordPress Tutorials
follow me on Twitter
Content and images are copyrighted by Tech-Evangelist.com and others

Copyright © 2022 Tech-Evangelist.com - All Rights Reserved
Posted code samples are free to use. Do not reproduce or republish articles or content on another web site.

Privacy Policy : Terms of Use