I presume you mean like a view-source option for the script itself? Well, if it is just for scripts you could do something like:

use CGI; my $q=new CGI; print $q->header("application/x-perl").`cat $0`;

Whereas if you're talking about *any* file (eg image files etc) you need to do a bit of juggling with mime types, and of course there's the whole attachment/filename issue also. Something like this would do for that:

use CGI; my $q=new CGI; my $filename=$q->param("some.param.name"); die "I won't display the file: $filename" unless some_test_or_other($f +ilename); print $q->header(-type=>`file -bi $filename`."; file=$filename", -content_dispostion=>"attachment; filename='$filename'"). `cat $filename`;

WARNING: Be very very careful if you do something like this. File-download scripts are responsible for a great many web site security holes, so unless you intend to impliment really anal input checking, don't even consider doing a (named) file download script as described above.

Also please note that using - and trusting - 'cat' and 'file' to do work for you is both inefficient and potentially insecure. They're included in these examples for brevity, in any real environment you should generally use a self-written function or CPAN module as appropriate.


In reply to Re: Downloading FIle from local harddrive? by rjimlad
in thread Downloading FIle from local harddrive? by ryza

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.