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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Listing with CGI & writing to text file/dir log

Status
Not open for further replies.

aenik

Programmer
Aug 22, 2002
7
US
Is there any way to read a folder's contents and display all the file names using CGI that will be hosted on the same server?
I need to be able to list the contents of a specific folder in a simple text file.
Thanks for any help/advice.
 
This should do the trick:
Code:
opendir DIR, $filedir or die "Could not open $filedir directory: $!";
while ( @files = readdir DIR ) {
    open(FILE, ">newfile.txt");
    print FILE @files;
    close(FILE);
}
closedir DIR;
 
thanks!
so to use this, i'll have to create a .cgi file like this:

#!/usr/bin/perl

# *** ***

opendir DIR, $filedir or die "Could not open $filedir directory: $!";
while ( @files = readdir DIR ) {
open(FILE, ">newfile.txt");
print FILE @files;
close(FILE);
}
closedir DIR;

and access it (assuming all access is enabled) from the browser like so: assuming i have a 'test' directory which has files, and a text file called newfile.txt exists in the same directory as the cgi file.
is that correct? thanks for the help. i greatly appreciate it.
 
Actually, your CGI would look more like
Code:
#!/usr/bin/perl

use CGI;
$q = new CGI;

# *** [URL unfurl="true"]http://www.tek-tips.com/gviewthread.cfm/lev2/4/lev3/31/pid/452/qid/342124[/URL] ***

print "Content-type: text/html\n\n";

$filedir = $q->param('filedir');

opendir DIR, $filedir or die "Could not open $filedir directory: $!";
while ( @files = readdir DIR ) {
    open(FILE, ">newfile.txt");
    print FILE @files;
    close(FILE);
}
closedir DIR;
//Daniel
 
Thanks to you both, PerlIsGood and Daniel! It works beautifully. I really appreciate it. Great forum!
 
Sorry, one last question. When calling the script like this:
it displays the contents of a 'test' folder located in the same directory (
How would I be able to specify a different folder location (for ex. " that is outside the cgi directory), and same goes with writing the text list file outside in the same album folder, outside the cgi-bin.
Thanks for you help. I cant seem to get the syntax correct.
 
Call it with filedir=../album and change the path to the newfile.txt to whatever you want. //Daniel
 
yeah that was it. obvious enough it seems.
i just need one more thing. this will be sweet if works:
i need to execute this cgi from an html page, for javascript processing of the returned string (i changed the printing from the text file to just simple print output).
i have not found any way to execute the cgi from javascript, so decided to just retrieve the list into the page for now. i changed the index page to .shtml to accept server side includes, and added this into my body:
<!--#exec cgi=&quot;/cgi-bin/album/dirlist.cgi?filedir=dir&quot;-->
i get a too familiar message of [an error occurred while processing this directive].. i tried changing the path to the full path, and nothing changed..then i removed the first slash. still nothing.
what is wrong with my ssi statement, and IS there any other, better way to retrieve the string right into javascript?
 
invalid CGI ref &quot;/cgi-bin/album/dirlist.cgi?filedir=dir&quot; in /home/dub/public_html/album/new/index.shtml

any ideas?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top