Main Content

Setting the file name for a downloaded document

Archive - Originally posted on "The Horse's Mouth" - 2005-11-03 16:34:26 - Graham Ellis

When you're writing a script to send a file to a browser, you can force it to be saved as a file using a content type header, and you can give it a name different to your script by using a content disposistion. In PHP, you might write

header("content-type: application/octet-stream\n".
"content-disposition: attachment; filename=\"$var.txt\"\n");

Remember - if you're going to call the header function, you MUST do it before you send any output to the browser!