Copy codeThe Code is as follows:
<? Php
$ File = 'monkey.gif ';
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;
}
?>
The above code is the download code
Next, paste the code for previewing PDF files online.
Copy codeThe Code is as follows:
<? Php
Public function fddAction ()
{
// Get attachment location
$ Attachment_location = $ _ SERVER ["DOCUMENT_ROOT"]. "/pdf/fdd/sample.pdf ";
If (file_exists ($ attachment_location )){
// Attachment exists
// Send open pdf dialog to user
Header ('cache-Control: public '); // needed for I. e.
Header ('content-Type: application/pdf ');
Header ('content-Disposition: inline; filename = "sample.pdf "');
Readfile ($ attachment_location );
Die (); // stop execution of further script because we are only outputting the pdf
} Else {
Die ('error: File not found .');
}
}
?>