Solved

PHP Dir List To Dropdown

Posted on 2008-10-26
2
273 Views
Last Modified: 2013-12-13
Hello Experts,

I have a PHP Script to read a Directorys contents which then makes a Drop Down selection of the files, the only problem with the script is it displays the . and .. directory structure links in the drop down which i dont want, i just want the files from the directory displayed, I have no idea how to get these out of the drop down...

Script is below.

Thanks,
Steve
<?php 
if ($handle = opendir('../files')) { 
    echo "<label><select name=\"URL\" id=\"URL\" >"; 
    while (false !== ($file = readdir ($handle))) { 
        echo "<option value=\"$file\">$file</option>"; 
    } 
    echo "</select></label>"; 
    closedir($handle); 
} 
?>

Open in new window

0
Comment
Question by:robsons995
2 Comments
 
LVL 8

Accepted Solution

by:
fcardinaux earned 500 total points
ID: 22806865

<?php 
if ($handle = opendir('../files')) { 
    echo "<label><select name=\"URL\" id=\"URL\" >"; 
    while (false !== ($file = readdir ($handle))) { 
        if ('.' === $file ¦¦ '..' === $file) continue;
        echo "<option value=\"$file\">$file</option>"; 
    } 
    echo "</select></label>"; 
    closedir($handle); 
} 
?>

Open in new window

0
 

Author Comment

by:robsons995
ID: 22806891
I was getting an error with the above script "Parse error: syntax error, unexpected T_STRING in..."

Changed to the one below and it works fine now, thanks for your quick and accurate help!!! :-)

Steve.
if ($handle = opendir('../files)) { 
    echo "<label><select name=\"URL\" id=\"URL\" >"; 
    while (false !== ($file = readdir ($handle))) { 
        if ('.' === $file) continue;
        if ('..' === $file) continue;
        echo "<option value=\"$file\">$file</option>"; 
    } 
    echo "</select></label>"; 
    closedir($handle); 
} 
?>

Open in new window

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Checking if varaible is empty 6 38
php extract($_REQUEST) 5 45
tutorial for ebay api 3 32
PHP Form Calculate Total Price 10 40
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now