Skip to content

Instantly share code, notes, and snippets.

@anova
Created February 14, 2014 09:35
Show Gist options
  • Save anova/8998317 to your computer and use it in GitHub Desktop.
Save anova/8998317 to your computer and use it in GitHub Desktop.
Force browser to download a pdf file.
<?php
if(!isset($_GET['f'])) die('You must specify which files to download');
$f = $_GET['f'];
if(!endsWith($f, '.pdf')) die('File MUST have .pdf extension (' . $f . ')');
$f_array = explode('/', $f);
$file_name = $f_array[count($f_array) - 1];
$file_url = $f;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
readfile($file_url);
//http://stackoverflow.com/a/10473026/181295
function endsWith($haystack, $needle)
{
return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment