CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
Displaying a directory



Dec 28, 2000, 1:04 PM

Post #1 of 3 (738 views)
Displaying a directory Can't Post

How do you dislay the list of files in a directory in an html page? Please help me!


Dec 29, 2000, 8:50 AM

Post #2 of 3 (728 views)
Re: Displaying a directory [In reply to] Can't Post

Well, it really depends on what type of files you want listed.
All files? If so, here is a quick and dirty script that should do
that. (note, all paths and filenames are generic and should
be altered to suit your site).

$dir= shift || '.';
opendir DIR, $dir or die "Can't open directory $dir: $!\n";
while ($file= readdir DIR) {
print "Found a file: '$file'\n";

Note that this will return the ENTIRE contents of the directory
(including the other directories '.' and '..')

In order to just find actual files, you might want to try
something like this:

while ($file= readdir DIR) {
next if $file=~/^\./;
print "Found a file: '$file'\n";

is a decent workaround for this.

I'm sure there are even better ways of doing this, but this particular method works for me.

living life -umop apisdn-


Dec 30, 2000, 9:30 PM

Post #3 of 3 (722 views)
Re: Displaying a directory [In reply to] Can't Post

But what about the files whose names begin with '.' ? You'll miss them with:


Rather do:

$file=~/^\.$/ || $file=~/^\.\.$/;

Dave (Wannabe Perl hacker). -


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives