Monday, 16 January 2012

How to make a download link for a file

This is extremely easy to do using PHP header.
First thing to do is to declare the content type, CSV in this case. If you want PDF, just use PDF instead of CSV.

header('Content-type: application/csv'); 

Then, specify the content disposition as attachment and the filename you want.

header('Content-Disposition: attachment; filename="downloaded.pdf"');

Just, read the file.
readfile('original.pdf');

A sample code,


<a href="?set=download">Click here to download</a>
Just check if the download is set using $_GET and thats it.

No comments:

Post a Comment