Forum Moderators: coopster

Message Too Old, No Replies

Drop-down list of files in folder

Drop down list of files in folder

         

Tourex

4:37 pm on Sep 13, 2005 (gmt 0)

10+ Year Member



I am preparing a form that includes a drop-down list consisting of the names of files in a certain folder on the server.

I only need to trap the file name (jpg), not the path and I'm not trying to upload the files or anything - simply allow the user to select one of a number of different image files (they're actually location maps).

Can anyone tell me how to achieve this, please.

jatar_k

4:56 pm on Sep 13, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



funny I was looking for another directory listing type function and passed by one that does what you need

this will read jpg's into an array, you will probably need to tweak it a bit but it works

$the_array = Array(); 
$handle = opendir('/root/path/to/dir');
while (false!== ($file = readdir($handle))) {
if ($file!= "." && $file!= ".." &&!is_dir($file)) {
$namearr = split('\.',$file);
if ($namearr[count($namearr)-1] == 'jpg') $the_array[] = $file;
}
}
closedir($handle);

Tourex

5:19 pm on Sep 13, 2005 (gmt 0)

10+ Year Member



Great - that works fine. Is there any way of sorting the results though? The list seems to be in a totally random order. I expected (wanted) the files to be listed in alphabetical order.

jatar_k

5:23 pm on Sep 13, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



just use sort() [php.net]

Tourex

7:25 pm on Sep 13, 2005 (gmt 0)

10+ Year Member



Brilliant! As you can tell, I'm new at this so thanks for your help - I've got exactly what I wanted.