arbruce has asked for the wisdom of the Perl Monks concerning the following question:

I'm having a problem with a download script only allowing the download of the Perl script instead of the actual file.
Here's the script:
#!/www/perl/bin/perl.exe -w # # filednload.pl # # File Download Utility # $server=$ENV{'SERVER_NAME'}; $browser=$ENV{'HTTP_USER_AGENT'}; $fileroot=$ENV{'DOCUMENT_ROOT'}; use CGI; use CGI::Carp qw(fatalsToBrowser); $cgiobject = new CGI; $dnloadfile = $cgiobject->param('filename'); $dnloaddir = $cgiobject->param('filedir'); $movecode = $cgiobject->param('movecode'); $usrcod = $cgiobject->param('usrcod'); $passwd = $cgiobject->param('passwd'); $userselect = $cgiobject->param('userselect'); $filekey = $cgiobject->param('filekey'); $program = "DBXDND"; $filename = $dnloadfile; $filename = lc($filename); $filepath = lc($fileroot . "/" . $movecode); $origpath = lc($filepath . "/" . $filename); $dnloadpath = $origpath; $flsiz = -s $origpath; print "Content-Type: application/octet-stream\n"; print "Content-Disposition:attachment filename=\"$filename\"\n"; print "Content-Length: $flsiz\n"; open (DLFILE,"<$origpath"); binmode DLFILE; binmode STDOUT; my $buffer = ''; while(read(DLFILE,$buffer,1024)) { print $buffer; } close DLFILE; $fileout=$fileroot . "/Temp/" . $usrcod . ".txt"; open (USER,">$fileout"); print USER "USRCOD=" . $usrcod . "\n"; print USER "PASSWD=" . $passwd . "\n"; print USER "BROWSER=" . $browser . "\n"; print USER "PROGRAM=" . $program . "\n"; print USER "FILENAME=" . $filename . "\n"; print USER "SERVER=" . $server . "\n"; print USER "DNLDDIR=" . $dnloaddir . "\n"; print USER "DNLDFILE=" . $dnloadfile . "\n"; print USER "DNLDPATH=" . $dnloadpath . "\n"; print USER "MOVECODE=" . $movecode . "\n"; print USER "USERSELECT=". $userselect . "\n"; print USER "FILEKEY=". $filekey . "\n"; close USER; print $cgiobject->redirect("http://" . $server . /cgi/cgi.exe?usrcod= +" . $usrcod);
I've already checked my server - Apache 2.2, running on Windows (Vista Business) - and it IS configured correctly for
running Perl scripts as CGI scripts.

Obviously, there something here I'm missing. Can anyone give me a hand?

Replies are listed 'Best First'.
Re: Help please...Downloading problem
by Joost (Canon) on Aug 20, 2007 at 17:54 UTC
    I've already checked my server - Apache 2.2, running on Windows (Vista Business) - and it IS configured correctly for running Perl scripts as CGI scripts.
    The most likely explanation of your problem is that you've NOT configured apache correctly. For one thing, the tree that the script is in should have the ExecCGI enabled, and have cgi-script handler enabled for whatever extension you're using for perl cgi scripts.

    In other words, you're not executing the script at all, just downloading it like a normal file.

      Not to rain on your parade, but...
      Here's the section from my httpd.conf file dealing with the CGI tree:
      ScriptAlias /cgi "/dropbox/cgi" <Directory "/dropbox/cgi"> Options +ExecCGI AllowOverride None Order allow,deny Allow from all AddHandler cgi-script .pl .php </Directory>

      And the bizarre thing is that I use other Perl scripts to update data files during the website use, and they all do exactly what I want them to do.
      Anton...
        Well, then I'll assume apache/perl/cgi works. Are you sure the file paths are correct? The code to make them looks slightly convoluted to me. Are you also sure you want to lowercase the whole file path? Make sure you check the result of open(), like open my $fh,"<",$whatever or die $!

        Also, you're missing a blank line at the end of the headers.

        Also also, this code will allow anyone to download any file off the server that the webserver has access to.

        update: and please use strict, it can catch typos that will take you (or us!) quite some time to find.

        update 2: its "content-disposition: attachment; filename=whatever" note the semi-colon and the lack of quotes.