Closed Thread Icon

Preserved Topic: list contents of a directory (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=21077" title="Pages that link to Preserved Topic: list contents of a directory (Page 1 of 1)" rel="nofollow" >Preserved Topic: list contents of a directory <span class="small">(Page 1 of 1)</span>\

 
JakeB
Paranoid (IV) Inmate

From: us
Insane since: Oct 2000

posted posted 10-07-2001 22:28

I'm trying to write a simple script where you give it the path, and the extension and it will output all the files and a link to each. (I can't get the variables to be the right path /var/www is always there) The url http://localhost/cgi-bin/listdir.cgi?path=/&type=html

code:
#!/usr/bin/perl -w

use strict;

my $html_head1='<html><head><title>';
my $html_head2='</title><link rel="stylesheet" href="../style.css"></head><body>';
my $html_foot='</body></html>';
my $dir_path;
my $http_path;
my @dir_list;
my $dir_item;
my $dir_filter;
my $temp;

my $key;
my $value;
my @param_pairs;
my %params;

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

@param_pairs = split(/\&/, $ENV{'QUERY_STRING'});
foreach $temp (@param_pairs) {
$temp =~ s/\+/ /g;
$temp =~ s/%(..)/pack("C", hex($1))/eg;
($key, $value) = split(/=/, $temp);
$params{$key}=$value;
}
$dir_path = $params{path};
$dir_filter = $params{type};

print "$html_head1$dir_path$html_head2\n\n";
$dir_path = "$ENV{'DOCUMENT_ROOT'}/$dir_path";
$dir_path =~ s/\/\/$/\//;
$http_path = "$ENV{'HTTP_HOST'}/$dir_path";
$http_path =~ s/\/\/$/\//;
print "$dir_path*$dir_filter<br>";
print "$http_path*$dir_filter<br>";
opendir(aDIR, $dir_path);

while ($dir_item = readdir(aDIR)) {
$_ = $dir_item;
if(/.*$dir_filter$/) {
push(@dir_list, $dir_item);
}
}
@dir_list = sort(@dir_list);
foreach $dir_item (@dir_list) {
print "<a href=\"http://$http_path$dir_item\">$dir_item</a><br>";
}

closedir(aDIR);
print "\n$html_foot\n\n";

outputs this:

/var/www/*html
localhost//var/www/*html
environment.shtml
index.html
index.shtml
test.html

but they are linking like this http://localhost//var/www/index.html when it should be http://localhost/index.html

Why can't I use opendir on http://localhost/? Do i have to use opendir on /var/www/path and then link with http://localhost/path?

Piper
Paranoid (IV) Inmate

From: California
Insane since: Jun 2000

posted posted 10-08-2001 01:14

Hi JakeB,

opendir needs the full system path to the directory. You will need to strip off your document_root from the path to create the urls. You need to make a copy of your path and create the url with somehting like this:

code:
my $url = $path;               #copy path to url
$url =~ s/$ENV{'DOCUMENT_ROOT'}//; #Get the doc_root outa the url
$url = "$ENV{'HTTP_HOST'}/$url"; #rewrite the full url


That's off the top of my head so test it out and see. There are several ways to do it though. Does that help at all?

Regards,
Charlie



[This message has been edited by Piper (edited 10-08-2001).]

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 10-08-2001 18:06

JakeB, why do you want to re-invent the wheel?

Apache will automatically show folder contents with appropriate links and even icons. Only thing that you should do (if you already didn't) is to define "Options Indexes" in your Apache configuration. You can also define this in .htaccess (if you have permission according to AllowOverride statement) for some specific folder only...


« BackwardsOnwards »

Show Forum Drop Down Menu