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
Categories: code A, action, action gt, amp, array, Art, bmp, Build, buildFileList, case, check, code, code lt, content, day, dropdown menu, Echo, Ed, exec, Execute, exis, extension, file, file names, filedropdown, fileInfo, filename, Files, folder, folder name, foreach, form, Free, free source, function, gif, hav, hii, IDE, image, images, initialize, Item, jpg, kadam, key, Li, list, lt, menu, method, name, natcasesort, NOT, Old, Open, option, option value, order, part, path, pathinfo, PHP, php session, png, POST, problem, proper path, ray, sagar, sagar kadam, scandir, Select, select name, selfile, Session, simple, Sort, source, start, theFolder, Today, Traverse, type, value, wat
Thanks Jigish
this work is done by Sagar
so thanks sagar
Aha, now that I fully comprehend what this could do, it seems to be a very handy code. Thanks a lot Sagar.