Generating dynamic files for download from PHP

If you have used my WordPress readme generator you would have noticed that I have a “Save As” button, which when pressed allows you to download the file which was just generated by the PHP code. (In most browsers this opens the save file as dialog box)

Recently one of the readers of my blog asked me how to do this in PHP and I was about to send him the code snippet. But then I thought of writing a blog article so that it will be useful for others as well.

So here we go.

The code is mostly self explanatory. The important part in the above code snippet is the “content Disposition” header.

Also since we are sending headers to the browser, we should not have started the output before this code executes. Otherwise we might get a “header already sent” error.

Related posts

Tags: , ,

0 Comments so far

Follow up comments through RSS Feed | Post a comment

2 Tweetbacks so far

Leave a Reply

Your email address will not be published. Required fields are marked *