in reply to File Download from CGI Script
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"; } } } } } }
|
|---|