Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re-direct after Download problem

by arbruce (Initiate)
on Aug 20, 2007 at 22:40 UTC ( #633959=perlquestion: print w/ replies, xml ) Need Help??
arbruce has asked for the wisdom of the Perl Monks concerning the following question:

Dear wise and powerful Monks of Perl,

I'm running a Perl script to download files from a website (and thank you again Joost for all your help), and it won't re-direct at the end of the script.

Given the script below (and the fact that I'm not a Perl but a Basis BBx/BBj programmer)...am I using the correct method of re-direct at the end of the script?
#!/www/perl/bin/perl.exe # # filednload.pl # # File Download Utility # use CGI; use strict "vars"; my $cgiobject = new CGI; my $filename = $cgiobject->param('filename'); my $filedir = $cgiobject->param('filedir'); my $movecode = $cgiobject->param('movecode'); my $usrcod = $cgiobject->param('usrcod'); my $userselect = $cgiobject->param('userselect'); my $filekey = $cgiobject->param('filekey'); my $server=$ENV{'SERVER_NAME'}; my $browser=$ENV{'HTTP_USER_AGENT'}; my $fileroot=$ENV{'DOCUMENT_ROOT'}; my $program = "DBXDND"; my $origpath = $fileroot . "/" . $filedir . "/" . $filename; my $flsiz = -s $origpath; print "Content-Type: application/octet-stream\n"; print "Content-Disposition: attachment; filename=$filename\n"; print "Content-Length: $flsiz\n\n"; open (DLFILE,"<$origpath") or die("cannot open file "); binmode DLFILE; binmode STDOUT; my $buffer = ''; while(read(DLFILE,$buffer,1024)) { print $buffer; } close DLFILE; my $fileout=$fileroot . "/Temp/" . $usrcod . ".txt"; open (USER,">$fileout"); print USER "USRCOD=" . $usrcod . "\n"; print USER "BROWSER=" . $browser . "\n"; print USER "PROGRAM=" . $program . "\n"; print USER "FILENAME=" . $filename . "\n"; print USER "SERVER=" . $server . "\n"; print USER "DNLDDIR=" . $filedir . "\n"; print USER "DNLDFILE=" . $filename . "\n"; print USER "DNLDPATH=" . $origpath . "\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);
Thanking you in advance for the help,

Anton...

Comment on Re-direct after Download problem
Download Code
Re: Re-direct after Download problem
by Errto (Vicar) on Aug 21, 2007 at 00:53 UTC

    What you're trying to do is not directly possible. Your CGI script can send the user a file or redirect them to another URL, but not both.

    Also this script is insecure, if for no other reason than that it will allow the user to read any file on the server machine so long as the web server process has access to it. Instead of doing that, have your script form the path to the file as you are doing, then use redirect to send the user to the actual URL of the file so they can download it directly, something like

    print $cgiobject->redirect("/$filedir/$filename");
    This has the additional benefit of giving the user proper headers for things like Content-type, Last-Modified etc.

    Update: made less punchy.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://633959]
Approved by calin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2014-10-25 15:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (145 votes), past polls