I have this code snippet I found here: How to make PDF file downloadable in HTML link?
I tried altering it since my PDF files are in a "resources" directory.
I thought I had it correct, but it is not working.
Can someone explain what I did wrong?
<?php
if (isset($_GET["file"]) && !empty($_GET["file"])) {
$file = $_GET["file"];
$path = "/resources/";
$getFile = $path.$file;
if (file_exists($getFile)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename=' . urlencode(basename($getFile)));
// header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($getFile));
ob_clean();
flush();
readfile($getFile);
exit;
}
}
?>
I added the isset part because without it, if there wasn't a variable set then it was throwing an error.
That works fine, but when I added $path = "/resources/"; and $getFile = $path.$file; it doesn't do anything(no errors either).
EDIT: What is not working? The files are not downloading.
Tested In: Internet Explorer and Google Chrome.
EDIT 2: The links on my pages look as follows:
<a href="?file=kansasHandbook.pdf">Download PDF File</a>