Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Downloading FIle from local harddrive?

by rjimlad (Acolyte)
on Sep 29, 2002 at 15:14 UTC ( [id://201553]=note: print w/replies, xml ) Need Help??


in reply to Downloading FIle from local harddrive?

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.

Replies are listed 'Best First'.
Re: Re: Downloading FIle from local harddrive?
by chromatic (Archbishop) on Sep 29, 2002 at 20:01 UTC

    Heh. Useless use of cat and backticks. Use seek to rewind DATA to position zero.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://201553]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-26 00:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found