Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How do I force a certain filename in the browser?

by cfreak (Chaplain)
on Jul 11, 2002 at 18:07 UTC ( #181095=perlquestion: print w/replies, xml ) Need Help??

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

Okay here's what I'm up to. I have some files that are saved in a directory not accessable from the webserver. (The files come from email attachments but that's beside the point). They are saved there because I only want to allow access to them by the user the owns them. So I have a script that authenticates the user and then retrieves the file for them and prints it to their browser (i have the filetypes saved in a database). If the mime type can be displayed it shows up and if now it asks the user if they want to download the file.

This all works great except that when the file needs to be downloaded the browser defaults to the name of the script rather than the filename of the file. I tried sending the filename in the content header, for example a Word Document:

print "Content-type: application/msword; filename=mydoc.doc\n\n";

However the browser seemed to ignore this. Is there a header that can tell it what filename to use or do I need to find another solution?.

BTW I thought about htaccess but then the user would have to authenticate twice as they need to beable to retrieve the file from inside the web based email client I'm working on, so its not really a solution.

Thanks in advance
Chris

Lobster Aliens Are attacking the world!

Replies are listed 'Best First'.
Re: How do I force a certain filename in the browser?
by Chmrr (Vicar) on Jul 11, 2002 at 18:15 UTC

    Use the Content-disposition header, like so:

    print "Content-type: application/msword\n"; print "Content-disposition: attachment; filename=mydoc.doc\n\n"

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Re: How do I force a certain filename in the browser?
by japhy (Canon) on Jul 11, 2002 at 18:31 UTC
    Use the $ENV{PATH_INFO} trick. Call your CGI program as http://www.foo.com/myprog.cgi/file.ext, and your web server will run myprog.cgi and store "/file.ext" in $ENV{PATH_INFO}. That's enough to fool the browser, and then you can use that value to fetch the file. (But keep security in mind, and don't allow them to put just ANY filename there!)

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

•Re: How do I force a certain filename in the browser?
by merlyn (Sage) on Jul 11, 2002 at 18:47 UTC
Re: How do I force a certain filename in the browser?
by cfreak (Chaplain) on Jul 11, 2002 at 19:49 UTC

    Thanks everyone. I got it by using the path info method. However out of curiosity, I'm passing parameters after the url so it looks something like this

    http://mysite.com/cgi-bin/script.cgi/file.doc?param1=value

    This works with my setup (apache) but how standard is it? Would it break in some configurations?

    Lobster Aliens Are attacking the world!
      With regards to the query string, this is entirely portable. The only thing that may possibly differ on various configurations is how they determine at what part of the request the script URI ends and the $PATH_INFO starts.

      Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2022-10-02 07:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (8 votes). Check out past polls.

    Notices?