in reply to directory list

Hmm. Most people so far missed an important part of this list:
I need to print out an html list of all images that are contained in a certain directory. Any suggestions on the best way of doing so?
Which probably means you want to make a link for each, and you're doing it by running a CGI script of some kind. See my snippet "Show a clickable link list of images from a CGI script" for some code to do this.

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
oh, an html list...
by elwarren (Priest) on Sep 20, 2000 at 19:09 UTC
    oh, an html list...
    perl -e 'print "<html><body><pre>",`ls *jpg *png *gif`,"</pre></body>< +/html>"'
RE: Re: directory list
by Adam (Vicar) on Sep 21, 2000 at 01:55 UTC
    Ooops.
    Well I'm not sure how much help jmac needed in the html department:
    use strict; use CGI; use CGI::Carp( 'fatalsToBrowser' ); use HTML::Entities; my $q = CGI->new(); my $dir = '\images'; # or some other default. chdir( $dir ) or die "Couldn't cd to '$dir', $!"; my @images = map {glob( "*.$_" )} qw( gif jpg jpeg tif xif bmp ); @images = map { encode_entities( $_ ) } @images; print $q->header(), $q->start_html(); print $q->h1( "My Image files" ); print "\n<UL>\n"; print "<LI>${_}\n" for @images; print "</UL>\n"; print $q->end_html();
    UPDATE:
    Rather then argue further with merlyn, I removed the user input part of my code. That's what I get for trying to add a little functionality... sigh.
      my $dir = $q->param('imageDir') ? $q->param('imageDir') : '\images'; # or some other default. die "Illegal directory name '$dir'" unless -d $dir; chdir( $dir ) or die "Couldn't cd to '$dir', $!";
      I'm not sure I'd ever install a CGI script that took an unrestricted directory name from a parameter! Danger, Will Robinson!

      -- Randal L. Schwartz, Perl hacker

        I disagree. There is no danger in that script. Granted I didn't do much of a test, but at least I check that it is really a directory. How much damage can a script do trying to change directory and glob for known extensions? Not much. In fact the only "risk" I can see would be some one looking to see if given directories exist on your server. Not that big a deal. Nor would it take much to add a regex after the -d line to make sure the user supplied directory is sanctioned.

        Calm your alarms merlyn, they are not needed here.