Home > code > Dropdown with the file names

Dropdown with the file names

hii guys today i came across a problem wherein i had to fill in a dropdown menu with all the file names in a particular folder. and this is wat i hav got from a free source and believe me guys this is as simple as you can imagine..

Code:

<?php
session_start();

//fills the dropdown with the files in a particular folder

function buildFileList5($theFolder) // Execute code if the folder can be opened, or fail silently
{
if ($contents = @ scandir($theFolder))
{
$found = array(); //initialize an array for matching files

$fileTypes = array('jpg','gif','png','bmp'); // Create an array of file types

$found = array(); // Traverse the folder, and add filename to $found array if type matches

foreach ($contents as $item)
{
$fileInfo = pathinfo($item);
if(array_key_exists('extension', $fileInfo) && in_array($fileInfo['extension'],$fileTypes))
{
$found[] = $item;
}
}

if ($found) // Check the $found array is not empty
{
natcasesort($found); // Sort in natural, case-insensitive order, and populate menu
foreach ($found as $filename)
{
echo "<option value='$filename'>$filename</option>\n";
}
}
}
}
?>

<form id="filedropdown" name="filedropdown" method="post" action="">
<select name="selfile" id="selfile">
<option value="">Select File</option>
<?php
buildFileList5('images'); //give the folder name here (be sure to provide proper path)
?>
</select>
</form>

Related Posts

  1. October 24th, 2008 at 23:11 | #1

    Thanks Jigish

  2. admin
    October 25th, 2008 at 12:47 | #2

    this work is done by Sagar
    so thanks sagar

  3. Rakesh Nair
    October 26th, 2008 at 09:57 | #3

    Aha, now that I fully comprehend what this could do, it seems to be a very handy code. Thanks a lot Sagar.

  1. No trackbacks yet.