By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,275 Members | 1,348 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,275 IT Pros & Developers. It's quick & easy.

Question: Does any know

P: 3
Does anyone know if perl can be used to hide a URL when someone downloads a file?
Mar 11 '08 #1
Share this Question
Share on Google+
4 Replies


KevinADC
Expert 2.5K+
P: 4,059
Yes it can.
Mar 11 '08 #2

P: 3
Great can anyone direct me to a module that helps do this? or a site where I can find more information on how to do this?
Mar 11 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
There is no module I know of that specifically does this. What you do is put your files in a directory that is not web accessible and have your perl script open the file and write the appropriate headers to download a file. The result is that the user has no idea where the file actually comes from. Here is a bare-bones script I wrote that does this:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -T
  2. use strict;
  3. use warnings;
  4. use CGI; 
  5.  
  6. my $q = CGI->new;
  7. my $file = param('file') or error('No file selected');
  8. my ($name,$ext) = $file =~ /^([^.]+)\.(.*)$/; 
  9. my $path_to_files = '/home/you/data';
  10. my $filesize = -s "$path_to_files/$file";
  11.  
  12. open(DLFILE, "<" , "$path_to_files/$file") or error('Error: Unable to find or open file');
  13.  
  14. print "Content-length: $filesize\r\n";
  15. print "Content-Type: application/x-download\r\n";
  16. print "Content-Disposition: attachment;filename=\"$file\"\r\n\r\n";
  17.  
  18. print <DLFILE>;
  19. close (DLFILE);
  20.  
  21. sub error {
  22.    print $q->header;
  23.    print shift; 
  24.    exit;
  25.  
The user selects a filename from a list of files in a form or hyper links, the field should be named 'file'.

Here you define the path to where the file are stored on the server:

Expand|Select|Wrap|Line Numbers
  1. my $path_to_files = '/home/you/data'; 
this should be above the web root. The script goes in the cgi-bin or wherever you run CGI scripts from. It is called from the forms action attribute or from the hyperlink:

Expand|Select|Wrap|Line Numbers
  1. <a href="download.pl?file=frog.jpg">download frog.jpg</a>
The CGI module can also write the headers for a file download you can look that up in the CGI documentation.
Mar 11 '08 #4

P: 3
This is a great help thank you very much Kevin. Very very helpful. :)
Mar 13 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.