Default filename in download dialog box

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jateeq
Forum Newbie
Posts: 14
Joined: Mon Mar 01, 2010 12:16 pm

Default filename in download dialog box

Post by jateeq »

Hello,

I have written a script to download files, but once the dialog box to prompt the user for a filename pops up, it has a .html extension by default, regardless of what the original extension was. Although not a big deal, it is annoying and inconvenient, and I'd like to display the original filename by default. Any ideas?
The code I'm using is as follows. The user clicks on a download link that loads download.php.

Download.php:
-------------------------------------------------------------------------------------------
header('content-type=text/xml');
header('content-disposition: attachment;filename = Copyright.xml');
readfile('Copyright.xml');
--------------------------------------------------------------------------------------------

NOTE: There are no errors. The file is downloaded properly.

Thanks,
Jawad
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Default filename in download dialog box

Post by requinix »

Some browsers use the filename given - if it's formatted properly (I suspect the spaces are causing problems)

Code: Select all

header('content-disposition: attachment;filename=Copyright.xml');
but some browsers don't use that, and prefer to use what's in the URL instead.

Also, the filename is only a suggestion. Browsers are free to use whatever name they want.
jateeq
Forum Newbie
Posts: 14
Joined: Mon Mar 01, 2010 12:16 pm

Re: Default filename in download dialog box

Post by jateeq »

So... it doesn't really make sense to define a filename if the browser's not going to follow it...

I've noticed that, when the dialog box pops up, by default *.html files is selected (display only html files). This is probably what's causing the filename to have an html extension by default. The problem now is, even though i switch the file type selection to list all files, it always defaults to listing only html files. Maybe i'll have to play with the browser settings... btw, i'm using Opera, but the same thing happens with firefox.

Jawad.
jateeq
Forum Newbie
Posts: 14
Joined: Mon Mar 01, 2010 12:16 pm

Re: Default filename in download dialog box

Post by jateeq »

I made the studpidest mistake :crazy: . There's supposed to be a colon in the headers ('content-type: text/xml) not ('content-type= text/xml');
Problem solved.


Jawad.
Post Reply