Skip to content

Instantly share code, notes, and snippets.

@freeeve
Created August 23, 2011 19:30
Show Gist options
  • Save freeeve/1166248 to your computer and use it in GitHub Desktop.
Save freeeve/1166248 to your computer and use it in GitHub Desktop.
Code to force download a file in PHP using header Content-Disposition: attachment
<?php
$file = 'filename.pdf';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
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($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment