Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4271908
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:30:22+00:00 2026-05-21T07:30:22+00:00

I searched from internet, but I only found php solutions to this problem. Please

  • 0

I searched from internet, but I only found php solutions to this problem. Please help if you know how to do this in perl.

I am trying to generate a webpage showing the content of a directory on my server’s local disk. For example, a page containing the following will do the work

<file name="file1" href="file1" />
<dir name="dir1" href="dir1/" />
<dir name="dir2" href="dir2/" />

Thank you for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T07:30:23+00:00Added an answer on May 21, 2026 at 7:30 am

    Modifications have to be made to secure the script and also in jailing it. However, the idea can be implemented like:

    #!/usr/bin/perl -T
    
    use strict;
    use warnings;
    
    use CGI;
    use File::Basename;
    use File::Spec;
    use Path::Trim;
    
    my $cgi = CGI->new();
    
    if ( my $file = $cgi->param('file') ) {
        open my $fh, '<', $file or die $!;
    
        print $cgi->header(
            '-type'           => 'application/octet-stream',
            '-attachment'     => basename($file),
            '-Content_Length' => -s $file,
        );
    
        binmode $fh;
        print while <$fh>;
    }
    else {
        my $path = $cgi->param('path');                                                                
    
        print $cgi->header(), $cgi->start_html();                                                                                               
    
        # remove redundant current directory and parent directory entries                                                                       
        my $pt = Path::Trim->new();                                                                                                             
        $pt->set_directory_separator('/');                                                                                                      
        $path = $pt->trim_path($path);                                                                                                          
    
      # remove all ../ and ./ that have accumulated at the beginning of the path and                                                            
      # make the path absolute by prepending a /                                                                                                
        $path =~ s{^ (\.\.? /)+ }{}x;                                                                                                           
        $path = "/$path" unless $path =~ m{^ / }x;                                                                                              
    
        print $cgi->h1($path);                                                                                                                  
    
        opendir my $dh, $path or die $!;                                                                                                        
        my @entries = grep { $_ !~ /^ \. $/x } readdir $dh;
        closedir $dh;
    
        print $cgi->start_ul();
        for my $entry ( sort { $a cmp $b } @entries ) {
            if ( -d File::Spec->catfile( $path, $entry ) ) {
                my $abs_entry = File::Spec->catfile( $path, $entry );
                my $anchor = $cgi->a( { 'href' => "?path=$abs_entry" }, $entry );
                print $cgi->li($anchor);
            }
            else {
                my $abs_entry = File::Spec->catfile( $path, $entry );
                my $anchor = $cgi->a( { 'href' => "?file=$abs_entry" }, $entry );
                print $cgi->li($anchor);
            }
        }
        print $cgi->end_ul(), $cgi->end_html();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i really need some help on this. I searched the internet but couldn't find
I searched a lot from internet for this thing. But everywhere very complex code.
Ive searched the internet, but found nothing pertaining to my question. From the link
I searched for this and found Maudite's question about text editors but they were
I searched and found this question but did not like the answer. Is there
I have searched internet and stackoverflow thoroughly, but I haven't found answer to my
I hate to ask but I've searched all over the internet trying to figure
I searched the net and handbook, but I only managed to learn what is
I searched google but couldn't find an answer to this rather simple question. I
I searched the web for examples of draggable Swing components, but I found either

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.