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.