Skip to content

Instantly share code, notes, and snippets.

@justmytwospence
Last active January 3, 2016 17:29
Show Gist options
  • Save justmytwospence/8495937 to your computer and use it in GitHub Desktop.
Save justmytwospence/8495937 to your computer and use it in GitHub Desktop.
This PHP function will initiate a direct download of a file (pdf, etc.). MIME type on line 5 will also need to be changed if not PDF (eg "application/xml"). Now just create a hyperlink directly to this php script.
<?php
$actual_file_name = "path/to/foo.pdf";
$saved_file_name = "foo-bar.pdf"
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename=$saved_file_name");
header("Content-Length: " . filesize($actual_file_name));
readfile($actual_file_name);
exit;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment