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

Hi Monks,

I am writing a small CGI script wherein the script will list all the files in a directory. I am able to list the files successfully but i am also trying to provide a link to the files which seems to have some problem.

In the below code, i have just showed the section where i provide the link to the file because i am able to get the list of files in the directory successfully

#!C:/Strawberry/Perl/bin/perl.exe use strict; use warnings; use diagnostics; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); my $cgi=new CGI; print $cgi->header(); print $cgi->start_html("My Submissions"); print "<body bgcolor=#c0c0c0>"; #Link to a sample file in the directory print "<h3><a href=\"file:///c:/temp/g.venkatesan/upload/test.txt\">Le +on</a></h3><br>"; print "</body>"; print $cgi->end_html();

The problem with this is, when i click on the link, the corresponding file is not opening. Infact, nothing is happening. But when i copy and paste the same link in a browser, the file is opening.

Could someone help me out with this issue? I have tried the code in both IE6 and Firefox 3.5.3

Thanks in advance!!!

Replies are listed 'Best First'.
Re: Problem linking a file in CGI script
by Corion (Patriarch) on Oct 29, 2009 at 16:30 UTC

    This is a security feature of your browser. In fact, you will find that your browser will also not open the linked file:// url if you serve it a plain HTML page instead of a Perl script. This is to prevent a malicious website from making you read/modify/open files with a known location on your harddisk (like ~/.ssh/id_dsa).

    To make your browser open file:// URLs from HTML pages served from a webserver, you will need to change the security settings of the browser.

      Hi,

      Firstly, thanks for taking time to reply.

      I tried changing the security settings in my browser but still i am not able to open the file. Also, changing the setting will not be a feasible solution as this program will be used by many people.

      Is there any other workarounds to display the files in a directory as well as provide a link to them?

        I'm not sure what you're actually trying to do. file:// URLs will only work on your local machine, nowhere else. Maybe if you explain what you're trying to do we can come up with a way to solve your actual problem.

        And for your reference, for example for FireFox, firefox open local file:// url points out the Mozilla Knowledge Base on this topic.

Re: Problem linking a file in CGI script
by jethro (Monsignor) on Oct 29, 2009 at 16:27 UTC
    Check out what the html file looks like in your browser. All browsers have a menu function to show you the raw HTML-code of a page.