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!

New to CGI need help with script 1

Status
Not open for further replies.

RJL1

Technical User
Oct 3, 2002
228
US
Sorry for the long post!!
I found this script that I have been modifying to learn some cgi. I would like to make one main modification but I'm not sure how to go about it. I have tried several thing but they have all been unsuccessful. The script lists all files in a directory and any sub directories with in that directory. It is the same as a simple FTP hierarchy but the look can be customized. The mod I want to make is to have all the files open in a new window instead of the main window.

Any help whit this is appreciated

Thanks
Ricardo

($action, $vir_path) = split(/&/, $ENV{'QUERY_STRING'});
$vir_path =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

if ($ENV{'QUERY_STRING'} ne "") {
if ($vir_path =~ /^\.\./ || $vir_path =~ /\/\.\.\// || $vir_path =~ /\.\.$/) {
print "Content-type: text/html\n\n";
print &header;
print "<font size=2 face=arial><b>You are not allowed to access a higher level directory with ..</b></font>\n";
print &footer;
exit;
}
else {
$path = "$basedir/$vir_path";
}
}
else {
$path = $basedir;
$action = "browse";
}

$thisdir = ($vir_path eq "") ? "" : "$vir_path/";
($action eq "browse") ? &browse : &get;

###############
sub browse {

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

opendir(BASE, $path);
@names = readdir(BASE);
closedir(BASE);

foreach $name (@names) {
$isdir = opendir(DIR, "$basedir/$thisdir$name");
if ($isdir == 1) {
push(@dirs, $name) unless ($name eq '.' || $name eq '..');
}
else {
push(@files, $name);
}
closedir(DIR);

(undef, undef, undef, undef, undef, undef, undef,
$size, undef, $modified, undef, undef, undef) = stat("$basedir/$thisdir$name");
($min, $hr, $day, $mon, $yr) = (localtime($modified))[1,2,3,4,5];
$min = "0$min" unless $min >= 10;
$hr = "0$hr" unless $hr >= 10;
$day = "0$day" unless $day >= 10;
$mon = "0$mon" unless $mon >= 10;
$filesize{$name} = $size;
$last_mod{$name} = "$mon/$day/$yr $hr:$min";
}

print &header;

$thisdir =~ s/ /\%20/g;

print "<table border=0 cellspacing=1 width=80%>\n";
print "<tr>\n";
print "<td align=left width=50%><font size=2 face=arial><b>Name</b></font>\n";
print "<td align=left width=20%><font size=2 face=arial><b>Last Modified</b></font>\n";
print "<td align=left width=20%><font size=2 face=arial><b>Size</b></font>\n";
print "</tr>\n";
print "<tr>\n<td colspan=3>\n";
print "<font size=1 face=arial><b>\n";
if ($vir_path ne "") {
$temp = $vir_path =~ tr#/#/#;
if ($temp > 0) {
$pardir = "?browse&".substr($vir_path, 0, rindex($vir_path, "/"));
}
else {
$pardir = "";
}
print "<img src=\"$imagedir/up.gif\" align=\"center\"><a href=\"$script$pardir\">Parent Directory</a>\n";
}
print "</b></font>\n";
print "</td>\n</tr>\n";

foreach $dir (sort @dirs) {
$dir_url = $dir;
$dir_url =~ s/ /\%20/g;
print "<tr>\n<td>\n";
print "<font size=1 face=arial><b>\n";
print "<img src=\"$imagedir/dir.gif\" align=\"center\"><a href=\"$script?browse&$thisdir$dir_url\">$dir</a></b></font>\n";
print "</td>\n";
print "<td align=left><font size=1 face=arial><b>$last_mod{$dir}</b></font></td>\n";
print "<td align=left><font size=1 face=arial><b>$filesize{$dir}</b></font></td>\n";
print "</tr>\n";
}

foreach $file (sort @files) {
$ext = substr($file, rindex($file, "\.")+1);
$type = "file";
foreach $filetype (@filetypes) {
if (lc($ext) eq $filetype) {$type = $filetype;}
}
$file_url = $file;
$file_url =~ s/ /\%20/g;
print "<tr>\n<td>\n";
print "<font size=1 face=arial><b>\n";
print "<img src=\"$imagedir/$type.gif\" align=\"center\"><a href=\"$script?get&$thisdir$file_url\">$file</a></b></font>\n";
print "</td>\n";
print "<td align=left><font size=1 face=arial><b>$last_mod{$file}</b></font></td>\n";
print "<td align=left><font size=1 face=arial><b>$filesize{$file}</b></font></td>\n";
print "</tr>\n";
}
print "</table>\n";

print &footer;

}

sub get {

if ($textviewer) {
$ext = substr($vir_path, rindex($vir_path, "\.")+1);
foreach $type (@texttypes) {
if ($type eq $ext) {
$flag = 1;
}
}
if ($flag) {
print "Content-type: text/html\n\n";
print &header;
open(FILE, "$basedir/$vir_path");
print "<font face=Courier size=2>\n";
$line = join("<br>", <FILE>);
$line =~ s/ /&nbsp;/g;
print $line;
print "</font>\n";
close(FILE);
print &footer;
exit;
}
}

print "Location: $baseurl/$vir_path\n\n";
}
 
This should do it..
Code:
($action, $vir_path) = split(/&/, $ENV{'QUERY_STRING'});
$vir_path =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

if ($ENV{'QUERY_STRING'} ne "") {
    if ($vir_path =~ /^\.\./ || $vir_path =~ /\/\.\.\// || $vir_path =~ /\.\.$/) {
        print "Content-type: text/html\n\n";
        print &header;
        print "<font size=2 face=arial><b>You are not allowed to access a higher level directory with ..</b></font>\n";
        print &footer;
        exit;
        }
    else {
        $path = "$basedir/$vir_path";
        }
    }
else {
    $path   = $basedir; 
    $action = "browse";
    }

$thisdir = ($vir_path eq "") ? "" : "$vir_path/";
($action eq "browse") ? &browse : &get;

###############
sub browse {

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

opendir(BASE, $path);
@names = readdir(BASE);
closedir(BASE);

foreach $name (@names) {    
    $isdir = opendir(DIR, "$basedir/$thisdir$name");
    if ($isdir == 1) {
        push(@dirs, $name) unless ($name eq '.' || $name eq '..');
        }
    else {
        push(@files, $name);
        }
    closedir(DIR);
    
    (undef, undef, undef, undef, undef, undef, undef,
    $size, undef, $modified, undef, undef, undef) = stat("$basedir/$thisdir$name");
    ($min, $hr, $day, $mon, $yr) = (localtime($modified))[1,2,3,4,5];
    $min = "0$min" unless $min >= 10;
    $hr  = "0$hr"  unless $hr  >= 10;
    $day = "0$day" unless $day >= 10;
    $mon = "0$mon" unless $mon >= 10;
    $filesize{$name} = $size;
    $last_mod{$name} = "$mon/$day/$yr $hr:$min";
    }

print &header;

$thisdir =~ s/ /\%20/g;

print "<table border=0 cellspacing=1 width=80%>\n";
print "<tr>\n";
print "<td align=left width=50%><font size=2 face=arial><b>Name</b></font>\n";
print "<td align=left width=20%><font size=2 face=arial><b>Last Modified</b></font>\n";
print "<td align=left width=20%><font size=2 face=arial><b>Size</b></font>\n";
print "</tr>\n";
print "<tr>\n<td colspan=3>\n";
print "<font size=1 face=arial><b>\n";
if ($vir_path ne "") {
    $temp = $vir_path =~ tr#/#/#;
    if ($temp > 0) {
        $pardir = "?browse&".substr($vir_path, 0, rindex($vir_path, "/"));
        }
    else {
        $pardir = "";
        }
    print "<img src=\"$imagedir/up.gif\" align=\"center\"><a href=\"$script$pardir\">Parent Directory</a>\n";
    }
print "</b></font>\n";
print "</td>\n</tr>\n";

foreach $dir (sort @dirs) {
    $dir_url = $dir;
    $dir_url =~ s/ /\%20/g;
    print "<tr>\n<td>\n";
    print "<font size=1 face=arial><b>\n";
    print "<img src=\"$imagedir/dir.gif\" align=\"center\"><a href=\"$script?browse&$thisdir$dir_url\" [b]target=\"_blank\"[/b]>$dir</a></b></font>\n";
    print "</td>\n";
    print "<td align=left><font size=1 face=arial><b>$last_mod{$dir}</b></font></td>\n";
    print "<td align=left><font size=1 face=arial><b>$filesize{$dir}</b></font></td>\n";
    print "</tr>\n";
    }

foreach $file (sort @files) {
    $ext = substr($file, rindex($file, "\.")+1);
    $type = "file";
    foreach $filetype (@filetypes) {
        if (lc($ext) eq $filetype) {$type = $filetype;}
        }
    $file_url = $file;
    $file_url =~ s/ /\%20/g;
    print "<tr>\n<td>\n";
    print "<font size=1 face=arial><b>\n";
    print "<img src=\"$imagedir/$type.gif\" align=\"center\"><a href=\"$script?get&$thisdir$file_url\" [b]target=\"_blank\"[/b]>$file</a></b></font>\n";
    print "</td>\n";
    print "<td align=left><font size=1 face=arial><b>$last_mod{$file}</b></font></td>\n";
    print "<td align=left><font size=1 face=arial><b>$filesize{$file}</b></font></td>\n";
    print "</tr>\n";
    }
print "</table>\n";

print &footer;

}

sub get {

if ($textviewer) {
    $ext = substr($vir_path, rindex($vir_path, "\.")+1);
    foreach $type (@texttypes) {
        if ($type eq $ext) {
            $flag = 1;
            }
        }
    if ($flag) {
        print "Content-type: text/html\n\n";
        print &header;
        open(FILE, "$basedir/$vir_path");
        print "<font face=Courier size=2>\n";
        $line = join("<br>", <FILE>);
        $line =~ s/ /&nbsp;/g;
        print $line;
        print "</font>\n";
        close(FILE);
        print &footer;
        exit;
        }
    }
    
print "Location: $baseurl/$vir_path\n\n";
}


M. Brooks
X Concepts LLC
 
Thanks so much I was going that route but had the quotes all wrong. Thanks again

Ricardo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top