Skip to content

Instantly share code, notes, and snippets.

@alwerner
Last active December 16, 2015 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alwerner/5429504 to your computer and use it in GitHub Desktop.
Save alwerner/5429504 to your computer and use it in GitHub Desktop.
Source: http://stackoverflow.com/questions/364946/how-to-make-pdf-file-downloadable-in-html-link "[...]run some sanity checks on the "file" variable to prevent people from stealing your files such as don't accept file extensions, add .pdf to the value."
<a href="pdf_server.php?file=pdffilename">Download my pdf</a>
// pdf_server.php
header("Content-Type: application/octet-stream");
$file = $_GET["file"] .".pdf";
header("Content-Disposition: attachment; filename=" . urlencode($file));
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Length: " . filesize($file));
flush(); // this doesn't really matter.
$fp = fopen($file, "r");
while (!feof($fp))
{
echo fread($fp, 65536);
flush(); // this is essential for large downloads
}
fclose($fp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment