Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rajvanshipradeep15/7dad502441ba1f24b06d55d81e7e5389 to your computer and use it in GitHub Desktop.
Save rajvanshipradeep15/7dad502441ba1f24b06d55d81e7e5389 to your computer and use it in GitHub Desktop.
download pdf inline and attachment
<?php
if( 1 == $_GET['file'] ) {
//file which we need to open in browser.
$filename="pdf_sample.pdf";
$header="Content-Disposition: inline; filename=$filename;"; // Send File Name
} else {
//password protected file
$filename="pdf_sample_protected.pdf";
$header="Content-Disposition: attachment; filename=$filename;"; // Send File Name
}
$file="./user_pdfs/$filename";
$len = filesize($file); // Calculate File Size
ob_clean();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type:application/pdf"); // Send type of file
header($header );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$len); // Send File Size
@readfile($file);
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment