Solved

PHP Dir List To Dropdown

Posted on 2008-10-26
2
269 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
Comment Utility

<?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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

771 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

11 Experts available now in Live!

Get 1:1 Help Now