Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

View a folder's contents... 1

Status
Not open for further replies.

aperfectcircle

Programmer
Apr 3, 2001
290
US
I found a thread from a couple years ago that described what I need to do (display all the files in a folder), but the problem is that the code is not idiot-proof enough for me...

Here's the script that was posted before:
$folder=$fullfilepath;
opendir (FOLDER, $folder) ¦¦ die "Unable to open your folder.";
@filenames= readdir(FOLDER);
closedir(FOLDER);

I tried altering this a bunch of different ways, but I won't bother embarassing myself by posting my attempts...

Can anyone tell me what needs to be done with this to make it display (on the web) the files in a folder I specify?
I'm running Perl on Windows NT (I don't know if that matters or not in this case) and I know absolutely nothing about Perl.

Thanks
 
How do you want to be able to specify the folder name? Will you be running this as part of a form on a web page, or do you just want to hard-code the folder name in the program?
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Hard-coding it into the script would be fine... It's easier and I don't think the folder will ever change or anything.
 
This will list the contents of a folder on your server. Right now it just uses <pre> to display them, but you could format the page as needed. Put the name of the folder you want listed in place of cgi-bin in the line that starts with $folder=
Code:
#!/usr/local/bin/perl

print &quot;Content-type: text/html\n\n&quot;;

print <<END;
<html><title>TEST FOLDER</title></head>
<body><pre>
END

# Change folder name below
$folder = $ENV{'DOCUMENT_ROOT'} . '/cgi-local';

opendir (FOLDER, $folder) or die &quot;Unable to open your folder.&quot;;
@filenames= readdir(FOLDER);
closedir(FOLDER);

foreach my $file (@filenames) {
   print $file, &quot;\n&quot;;
}

print <<END;
</pre>
</body>
</html>
END

exit 0;

#------------------------------------------------------
1;
If you don't want the '.' and '..' shown, add this line right after the foreach:
Code:
   next if $file =~ /\A\.{1,2}\Z/;
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
My pleasure. :) Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top