Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Recursive CGI Dirlist

by Ranna (Acolyte)
on Oct 24, 2001 at 12:55 UTC ( #121033=sourcecode: print w/replies, xml ) Need Help??
Category: CGI Programming
Author/Contact Info Ranna -, Ranna@FluffMUCK, Ranna@FurryMUCK, RannaFox@SPR
Description: I claimed that my site was just a bunch of general content, and...well, it is. No specific theme or anything. Because of this, I just sorted my public_html directory (sorta...still really messy) and let this thing loose on certain parts of it. The nice part is, it skips all those private directories that the world really would be better without. :o)

This is the final (I hope) version, but input is welcome. Example: Available code directory

# RedFox! Productions: DirList
# All coding done by one Ranna Fox

use strict;
use CGI qw( :standard );

my $file = param('d') || "prefix/";
if ($file =~ /^\//) {
  print "<b> may not list files with absolute paths.  Especi
+ally not <em>that</em> absolute path</b>\n";
} elsif ($file =~ /\.\./) {
  print "<b>'Scuse me, I won't list directories with .. in their path.
+  Better fix that.</b>\n";
} elsif (not $file =~ /^prefix\//) {
  print "<b>See, the way things work is that if you're gonna list a di
+rectory, you gotta put a 'prefix/' before it, which means it has to b
+e in my directory.<b>\n";

my $filen;
($filen = $file) =~ s{^prefix/}{/path/to/home/dir/};
if (not -d $filen) {
  print "<b>That...uh...doesn't appear to exist... o.O</b>\n";

&parse_dir($filen, $file);

sub parse_dir {
  my $dir = $_[0];
  my $req = $_[1];
  opendir(PARSEDIR, "$dir") || die "Couldn't open directory $dir: $!\n
  my $currdir;
  $dir =~ s/(.*)\/$/$1/;
  ($currdir = $dir) =~ s{.*/([^/]*)$}{$1}; 
  print "<li><b>$currdir/</b>\n<ul>\n";
  foreach my $file (sort(readdir(PARSEDIR))) {
    my $fullname = $dir . "/" . $file;
    next if ($file =~ /^\..*/ || ! -r $fullname);
    if (-d $fullname) {
      $req = $req . "/" . $file;
      &parse_dir($fullname, $req);
    } else {
      my $dirn;
      ($dirn = $dir) =~ s{/path/home/to/dir}{};
      ($filen = $file) =~ s/ /\%20/g;
      print "<li>[<a href=$dirn/$filen>Bare link</a>]";
      if (-T $fullname) {
        # showtext.php is basically an fpassthru() enclosed in <pre> t
        print "[<a href=/showtext.php?file=prefix$dirn/$filen>Online</
      } else {
        print "[<a href=$dirn/$filen>Download</a>]";
      print " - $file</li>\n";
print "</ul>\n";
Replies are listed 'Best First'.
Re: Recursive CGI Dirlist
by davorg (Chancellor) on Oct 24, 2001 at 12:57 UTC

    I think perhaps that you should take a look at File::Find as it would make your life a lot easier :)


    "The first rule of Perl club is you don't talk about Perl club."

      I know...but it wouldn't have saved much...I still would've had to write the subroutine for the formatting, and I didn't have permissions to install at the time. Now, I'm an admin for some reason... gives me permission to listen to music on the server and not do C++ work.

        You don't need permission to install anything. File::Find has been a standard part of Perl forever.

        And as a bonus, it Does The Right Thing with symbolic links - which your code doesn't seem to.


        "The first rule of Perl club is you don't talk about Perl club."

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://121033]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2021-09-18 05:03 GMT
Find Nodes?
    Voting Booth?

    No recent polls found