Article
Build a File Download Script in Perl
Page: 1 2
Logging The Download
The next 3 lines are optional, but they’re useful for keeping track of the number of times each file is downloaded. The 3 lines simply open the log file called “dl.log” (or display an error), print the file name, and close the log file. As before, you must specify the absolute path on your server.
open (LOG, ">>/home/files/dl.log") || Error('open', 'file');
print LOG "$ID\n";
close (LOG);
For this to work, 2 things must be in place. First, the directory must already exist on your server. Second, the file “dl.log” must exist in the directory and be writable. To change the file permissions on a Linux/Unix server, you must use the command chmod as follows: chmod 777 dl.log/.
Sending The File
Now, we want to send the file to the user. What if the file is an image, PDF, or some other file type that the browser recognizes? Even though we've masked the URL, the visitor's browser will display the file. To coax the browser into displaying the "save as..." dialog box, we need to fake the file type in the header that’s sent:
print "Content-Type:application/x-download\n";
print "Content-Disposition:attachment;filename=$ID\n\n";
print @fileholder
}
Some Error Handling
We have directed the script to call a subroutine if something should go wrong when the file is opened, or the download is recorded to the log file. This next section of code displays the error in the browser window:
sub Error {
print "Content-type: text/html\n\n";
print "The server can't $_[0] the $_[1]: $! \n";
exit;
}
Done! But How Does It Work?
To test it out, upload the script to your cgi-bin directory and chmod it to 755.
Now, how do we call this script? There are a couple of ways to do this. First, you can create a hyperlink in your Web page that reads:
<a href="/cgi-cin/script.cgi?ID=file.zip">Download File</a>
Or, you can create a form that includes a field (checkbox, dropdown menu, etc.) called ID, and an action of: <http://www.yourserver.com/cgi-bin/script.cgi> /cgi-bin/script.cgi
Source Code
Below is a sample HTML Webpage that you can use as a template. This sample provides a dropdown menu through which the user can select the file they’d like to download.
<html><head>
<title>Download Page</title>
</head>
<body>
<form action="/cgi-bin/script.cgi">
<p>
<select size="1" name="ID">
<option value="0001.jpg">File One
</option>
<option value="file.zip">File Two
</option>
</p>
</select>
<input type="submit" value="Submit" name="B1">
</form>
</body>
</html>
Here’s the complete script:
#!/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
}
Don't forget to download the code in full!