<a href="http://www.yoursite.com/cgi-bin/download.cgi?filename=somefil +e.txt"> Download somefile.txt </a> #!/usr/bin/perl -wT use strict; use CGI; # clean up the environment for CGI use BEGIN { delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; $ENV{'PATH'} = '/bin:/usr/bin:'; } $CGI::DISABLE_UPLOADS = 1; $CGI::POST_MAX = 1024; my $q = new CGI; my $filepath = '/vs/www.yoursite.com/'; my $filename = $q->param('filename'); # stop backing up path with filname like ../../../etc/passwd invalid($filename) if $filename =~ m|../|; # untaint $filename - allow alphanumerics . - and / in name ($filename) = $filename =~ m|^([\w\d.-/]+)\z|; download_file( $filepath, $filename ); exit; sub download_file { my ($filepath,$filename) = @_; invalid($filename) unless -e $filepath.$filename; my $filesize = -s filepath.$filename; # could use _ print "Content-disposition: attachment; filename=$filename\n"; print "Content-Length: $filesize\n"; print "Content-Type: application/octet-stream\n\n"; my $buffer; open FILE, $filepath.$filename or die "Oops $!"; binmode FILE; binmode STDOUT; print $buffer while read(FILE, $buffer, 4096); close FILE; } sub invalid { my $filename = shift; print $q->header, "<p>$filename does not exist on this server"; exit; }

This will send a specific file with its name to the browser. A word of warning. First we need to hardcode a $filepath into the script. Second we need to stop someone downloading $filename = '../../../etc/passwd'.

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print


In reply to Re: Upload-dialog and CGI-download possibilities by tachyon
in thread Upload-dialog and CGI-download possibilities by kodo

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.