LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl Script to download file (https://www.linuxquestions.org/questions/programming-9/perl-script-to-download-file-373875/)

apt 10-17-2005 06:18 AM

Perl Script to download file
 
I have a perl script that has become available through my Hosting company.

Its function is to send and email to a user and then redirects to a another page. And its has no problem with it.

Code part which redirects to another page.
Code:

#### Redirect if "next-url" is included
if ($FORM{'next-url'}) {
    print "Location: $FORM{'next-url'}\n";
    print "\n";
    exit;
}

But now I want it to do a download first, before it redirects to the other page.

Is that possible, in perl script.?



Thanks in advance.

J_K9 10-18-2005 03:43 PM

It is possible, although I know _no_ Perl whatsoever. :) Take a look at this script I found:
Code:

Code from File Download Script blahblahblah...

#!/usr/bin/perl -wT

use CGI ':standard';
use CGI::Carp qw(fatalsToBrowser); 

my $files_location; 
my $ID; 
my @fileholder;

$files_location = "/usr50/home/webdata/photos";

$ID = param('ID');

if ($ID eq '') { 
print "Content-type: text/html\n\n"; 
print "You must specify a file to download."; 
} else {

open(DLFILE, "<$files_location/$ID") || Error('open', 'file'); 
@fileholder = <DLFILE>; 
close (DLFILE) || Error ('close', 'file'); 

open (LOG, ">>/usr50/home/webdata/public_html/test.log") || Error('open', 'file');
print LOG "$ID\n";
close (LOG);

print "Content-Type:application/x-download\n"; 
print "Content-Disposition:attachment;filename=$ID\n\n";
print @fileholder
}

Ok, it does much more than what you want to do, but I think that if you take a better look at it you may be able to understand some parts (ie. the bits you need).

I _think_ that you'd need to put in something like the following:
Code:

#Declare a variable
my @fileholder;

#Download the file
print "Content-Type:application/x-download\n"; 
print "Content-Disposition:attachment;filename=$ID\n\n";
print @fileholder

Please note that I don't have the foggiest idea if that'll work or not, I just didn't want to leave this thread unanswered. :)

M.

apt 10-19-2005 08:19 AM

Thanks J_K9 for your prompt reply.

Code:

#### Redirect if "next-url" is included

#Download the file
print "Content-Type:application/x-download\n"; 
print "Content-Disposition:attachment;filename=$ID\n\n";
print @fileholder

if ($FORM{'next-url'}) {
    print "Location: $FORM{'next-url'}\n";
    print "\n";
    exit;
}

I tried with the code u had mentioned to download the file before redirecting to next URL. It just downloading a file or redirecting to another URL.

I want it to do both the tasks at a time. i.e to start download a file first and then it should be redirected to other page.

Any solution?

J_K9 10-19-2005 08:25 AM

I don't know any Perl whatsoever, so I'm sorry about that. I guess you could set a timer on the redirect so that it waits a while for the download to finish. (You could also supply a link on the page for the redirect to be made manually if it is taking too long because of the timer). I admit this way is very crude, but it's the only thing I can think of.

I really have a clue what functions are pre-available in Perl, sorry. You could always make the program go on a loop until it can 'see' that the file's been downloaded! :D Hehe....

<experienced Perl programmer please post below> :)

apt 10-19-2005 08:33 AM

Thanks J_K9 for your suggestions.

Ok i will look into other possible solutions before continuing this one.


All times are GMT -5. The time now is 07:41 AM.