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

I'm trying to log downloads of certain files, so I ran to the type of http://somewhere.com/fetch.pl?id=Application1, this is how I thought of it :

I then wanted to restrict downloads through this script, so that people cannot know the real location of the file and download it directly. so what I need is: have a file called foo_application1_bar.zip (or other) and then when I spit the contents to the browser I want to specify the filename to save with ex: Application1.zip

I wrote a little script to test with :

#!/usr/bin/perl -w open (ZIP, "test.zip"); binmode(ZIP); @all = <ZIP>; close(ZIP); print "Content-type: application/zip\n\n"; foreach (@all) { print; }

this ran, I have a "save file to disk" message with the default filename fetch.pl and having the .pl extension associated with a prog on my pc, I couldn't change the extension. but when I renamed the file to fetch.zip it opened and was the file requested.


How can I specify the filename to save? I think it's a header but I can't find it.

Note: I know that the script is badly programmed and I should die on open failure and stuff, but as I said I was only testing.
Chady | http://chady.net/

Replies are listed 'Best First'.
(jeffa) Re: Specifying filename for download
by jeffa (Bishop) on Mar 02, 2001 at 00:39 UTC
    If you use CGI, you can utilize the 'redirect' method:
    use strict; use CGI; my $CGI = new CGI; if (my $file = $CGI->param('file')) { print $CGI->redirect('http://localhost/'.$file); } else { print $CGI->header; print "stuff"; }
    but I would not trust the user - make sure you untaint that file name first.

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--
    
Re: Specifying filename for download
by mr.nick (Chaplain) on Mar 02, 2001 at 02:47 UTC
    You can specify the filename for downloading using the "Content-Disposition" header. Like this:
    #!/usr/bin/perl -w open (ZIP, "test.zip"); binmode(ZIP); @all = <ZIP>; close(ZIP); print "Content-type: application/zip\n"; print "Content-Disposition: filename=test.zip\n"; print "\n"; foreach (@all) { print; }
Re: Specifying filename for download
by merlyn (Sage) on Mar 02, 2001 at 00:37 UTC
Re: Specifying filename for download
by sierrathedog04 (Hermit) on Mar 02, 2001 at 03:19 UTC
    I then wanted to restrict downloads through this script, so that people cannot know the real location of the file and download it directly.

    It seems to me that web users will only be able to download your files directly if you place them in a virtual directory or if you have ftp turned on. I know from experience that is the case with the Oracle Application Server webserver—it will reject as invalid any path that you have not previously specified as a valid one.

    Pulling out my copy of Apache Server for dummies, I see that such is the case for Apache Server as well. The book says that:

    The resource configuration file, srm.conf: This file usually contains things that define where documents are found, how to translate Web addresses to file names, and so on
    Your Perl script will know the locations of your files and can open and read them server side. Your web users will not be able to download the files because there will not be any valid URL pointing to them.