CodersAdvocate

I work hard, so you don't have to

Content Disposition PHP

 
One of the most important tools that we have available as a web coder is this thing called "Content-Disposition".

What is it?

Content disposition is a way of tricking the users web browser in to thinking the "content" the browser is about to recieve is actually something else.
A simple example of this can be found in one of my other blogs about the VCalendar thingy in outlook.

There are a few things to take into consideration about using Content Disposition. Firstly is the program that you're trying to download to be installed on the users computer?
Secondly, is your file safe and is content disposition really what you want to do?

In order to make the browser think that the code you've written is something else you have to modify a few headers,


header("Content-type: application/msexcel");
header("Content-Disposition: attachment; filename=worddoc.xml");

The above PHP will trick the browser into thinking its a msexcel file as opposed to the standard headers of text/xml, txt text/html.

With Disposition the possibilities are endless, you can even write bytes out and download entire files down to the user.

Copyright 2011 CodersAdvocate ©