http://www.perlmonks.org?node_id=796543

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

Hi All,

I could open a file under CGI site folder. But, I could not open a physical file which is not under the CGI site (eg c:\somefolder\somefile.txt) through href

my coding is as follows. please guide me where i made worng.

#!c:/perl/bin/perl.exe use strict; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser warningsToBrowser); my $file = 'undersite/1.txt'; my $html = new CGI; print $html->header('text/html'); print $html -> start_html(); print $html -> start_form(); print $html -> p("Hi"); print $html -> p({}, "<a href=\"$file\">text file link</a>"); print $html -> end_html(); print $html -> end_form();


Thanks in advance,
Shanmugam A.

Replies are listed 'Best First'.
Re: could not open a physical file using href
by CountZero (Bishop) on Sep 21, 2009 at 17:55 UTC
    It is not because out of laziness or convenience a great many number of URLs bear a close resemblance to the file which is finally served by the web-server, that there is any necessary link between the URL and such file. As a matter of fact, your above CGI-script is most likely not called 'undersite/1.txt' or '1.txt', but rather 'something.pl' or 'something.cgi' or anything else you have your web-server configured to accept as the link to your script.

    It has been suggested that perhaps you should use the file:// URL-scheme, but beware, this is likely to work only if the file is on the same computer the web-page is being viewed upon. RFC 1738 says as follows about this scheme:

    3.10 FILES

    The file URL scheme is used to designate files accessible on a particular host computer. This scheme, unlike most other URL schemes, does not designate a resource that is universally accessible over the Internet.

    A file URL takes the form:

        file://<host>/<path>

    where <host> is the fully qualified domain name of the system on which the <path> is accessible, and <path> is a hierarchical directory path of the form <directory>/<directory>/.../<name>.

    For example, a VMS file

      DISK$USER:[MY.NOTES]NOTE123456.TXT

    might become

      <URL:file://vms.host.edu/disk$user/my/notes/note12345.txt>

    As a special case, <host> can be the string "localhost" or the empty string; this is interpreted as `the machine from which the URL is being interpreted'.

    The file URL scheme is unusual in that it does not specify an Internet protocol or access method for such files; as such, its utility in network protocols between hosts is limited.

    The very last sentence is of course the most important.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: could not open a physical file using href
by ikegami (Patriarch) on Sep 21, 2009 at 15:09 UTC
    Given your meek description of the error ("I could not open"), it's hard to know what the problem is.
      Hi

      the link code is
      print $html -> p({}, "<a href=\"D:/test.pdf\">file link</a>");


      While i click the link it shows as "Firefox doesn't know how to open this address, because the protocol (d) isn't associated with any program."

      Note
      But if the file is under the CGI site then it opens.

      Thanks
      Shanmugam A.

        That's not the code you gave us originally.

        Since this is a CGI script, I'm assuming it's output is being fetched using HTTP. (HTTPS is the next most likely candidate. Consider HTTP and HTTPS interchangeable.)

        href takes a URL for value. I've never heard of the "d:" URI/URL scheme either. Your URL is unsupported or malformed.

        • If that's the absolute file system path to a file on the client's computer, you'd use the "file:" scheme (but that makes no sense and some browsers won't let you for security reasons).
        • If that's the absolute file system path to a file on the server's machine, you'll need to construct an "http:" URL.
        • If it's suppose to be a relative URL, then you need to escape the ":".

        Let me know which one it is, and I'll help you further.

Re: could not open a physical file using href
by Anonymous Monk on Sep 21, 2009 at 15:06 UTC
    You shouldn't even try