I know this is a really old post, but figured I'd post anyways. I've always had good luck with the following. It works for binary and for ASCII files.

File to be downloaded is passed to the script as part of the query str +ing: $qs{filename} my $docspath = "/home/acctname/public_html/userdata/user_" . $cust +omer{myid} . '/dlfiles'; if(-d "$docspath") { opendir(READ, "$docspath"); my @files = readdir(READ); closedir(READ); my %relative_mimetype = ( 'gif' => 'image/gif', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg', 'png' => 'image/png', 'bmp' => 'image/bmp', 'doc' => 'application/msword', 'docx' => 'application/x-msword', 'pps' => 'application/mspowerpoint', 'ppt' => 'application/powerpoint', 'pdf' => 'application/pdf', 'txt' => 'text/plain' ); for my $dlfile (@files) { unless($dlfile =~ /^(\.|\..)$/) { if($dlfile =~ /^ **regex to test file name to make s +ure they are downloading what they are allowed do** /) { my @nameparts = split(/\_/, $dlfile); my $marker = shift @nameparts; my $dl_name = join('_', @nameparts); my ($filename, $ext) = split(/\./, $dl_name); if($dl_name eq $qs{filename}) { my @extension = split(/\./, $nameparts[$#namep +arts]); my $mimetype = $relative_mimetype{ lc($extensi +on[$#extension]) }; $mimetype ||= "application/octet-stream"; print "Pragma: no-cache\nContent-Type: $mimety +pe\nContent-Disposition: attachment; filename=$dl_name;\n\n"; if(open(FILE, "<$docspath/$dlfile")) { if(-B "<$docspath/$dlfile") { binmode(STDOUT); } while(<FILE>) { print $_; } close FILE; exit; } else { print "Pragma: no-cache\nContent-type: tex +t/html\n\Unable to open file. Please contact the Site Adminsitrator. +(ErrNo.3)\n\n"; } } } } } }

In reply to Re: File Download from CGI Script by TorontoJim
in thread File Download from CGI Script by Anonymous Monk

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.