Skip to content

Instantly share code, notes, and snippets.

@bskiefer
Forked from myselfsufiyan/.php
Created January 24, 2014 16:05
Show Gist options
  • Save bskiefer/8600286 to your computer and use it in GitHub Desktop.
Save bskiefer/8600286 to your computer and use it in GitHub Desktop.
<?php
// place this code inside a php file and call it f.e. "download.php"
$path = $_SERVER['DOCUMENT_ROOT']."/path2file/"; // change the path to fit your websites document structure
$fullPath = $path.$_GET['download_file'];
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
break;
default; // Other document formats (doc, docx, odt, ods etc)
header('Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
}
header('Content-Description: File Transfer');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
ob_clean();
flush();
readfile($fullPath);
}
fclose ($fd);
exit;
// example: place this kind of link into the document where the file download is offered:
// <a href="download.php?download_file=some_file.pdf">Download here</a>
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment