masood91 has asked for the wisdom of the Perl Monks concerning the following question:

Respected Perl Monks! I am a Perl beginner with just a few days of perl learning experience. I am trying to create a CGI script which can display files in a folder and after clicking the file should be downloaded. I can see the list of files, but i am unable to download the file. Can you please help? here is my script:

#!C:/Dwimperl/perl/bin/perl.exe use 5.010; use strict; use warnings; use CGI; my $dir='C:/Perl64/bin/'; opendir (DIR, $dir); my $file=readdir DIR; my $cgi = CGI->new(); print $cgi->header(),$cgi->start_html(); print $cgi->start_ul(); while ($file = readdir DIR) { #print $cgi->h1($file); my $anchor=$cgi->a({ 'href' => "$dir"."$file" }, $file); print $cgi->li($anchor); } closedir DIR; print $cgi->end_ul(),$cgi->end_html();

When I click on blue anchors, nothing happens!

  • Comment on Perl cgi script for directory listing and file downloading!! Problem: Unable to download the file
  • Download Code

Replies are listed 'Best First'.
Re: Perl cgi script for directory listing and file downloading!! Problem: Unable to download the file
by Corion (Patriarch) on Nov 05, 2014 at 11:34 UTC

    What is the URL for the files as you see them in the browser?

    The URL needs to be a http:// URL, not a filename local to your server machine.

      I am running my script via WAMP server, with the script file located in /www folder. Where should I put my files, so that they can be downloaded.!! ??

        This is a browser configuration issue.

        I guess that, for example, /www/download might be a good place for your files.

        You need to learn about how your webserver maps URLs to files and appropriately configure the webserver. First get the URL download working without your script by going directly to the URL with your browser. Then you can generate the URLs from your script.

Re: Perl cgi script for directory listing and file downloading!! Problem: Unable to download the file
by roboticus (Chancellor) on Nov 05, 2014 at 11:35 UTC

    masood91:

    I'm still a web-noob at the moment, so I don't know for certain. But when looking at your code, I'm thinking you might find the problem if you ask yourself a couple questions:

    • Where on your server is your script located?
    • What URL do you use to access your script?
    • How is that different from a link like: <a href="C:/Perl64/bin/xyz.exe">xyz.exe</a>?

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      I am using Wamp server, so the script is located in WWW folder. I am using localhost:8080/directory.pl to access my script in browser. As pointed in earlier comment my files are located in local machine, where in wamp folder should i put te files, so that they will be accessible to script?

        masood91:

        It could be anywhere ... that would depend on how the web server is configured. Check with the person who set up your WWW server to find out which of the directories on your machine are mapped to be accessible from the WWW server, and which URLs the directories would correspond to.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: Perl cgi script for directory listing and file downloading!! Problem: Unable to download the file
by Anonymous Monk on Nov 06, 2014 at 01:21 UTC