Setting the Download Name of a File
PHP Tricks

Main Page

Linux
News Links
Web
PHP
Game Links
Search
 Google

Food
 Epicurious
 S.O.A.R.

Music
 BMG
 Play

TV/Movies

PDX
Real Estate
Funny
Health
Travel
Stores
Lighting
PIC
Misc.

About This Site

 

 

Ever wondered how to set the name of a download file? There are two ways to do this. One way is to pass the name of the file in the header. Here is some sample code:
header( "Content-type: application/x-something" );
header( "Content-Disposition: attachment; filename=test.txt" );
header( "Content-Description: PHP3 Generated Data" );
If you want the file to be saved, the content type needs to be something that the browser does not recognize. You can also send a known 'Content-type' and have the browser do the right thing.

Click here to see this in action.
Click here to see the source.

 

Another method is to send a "location" header with the url of the desired file.

Some more notes:

  • The other thing that I found out about IE 5.5 is that going directly to the file i.e. http://host/setfile.php works but going to http://host/ and then clicking on the "setfile.php" file gives weird results. In the second case you have to select "Open" (NOT Save) and then another dialog will appear where you can click "Save" and then the file name will be correct.
  • This does not seem to work with Konqueror.


This page has only been accessed 1 time today by 1 hosts.
  And 4 times in the past seven days by 4 hosts during those seven days.

Disclaimer: All links and information is provided "As Is". A companies and/or product linked to or mentioned on this site does not represent an endorcment of said item. Use at your own risk. Send any questions, comments, complaints, etc. to: The Webmaster (Yes the nospam-... is a real email address)