Do you work with datasheets regularly ? Always getting .pdf files open in your browser and having to wait EONs for them to download/display ? Well here is a resolve for that most frustrating of problems with Adobe's "Handy file intercept" feature. Use this script to FORCE the browser to download the file rather than display it inline.

// html file with links to either display or download the .pdf file.
<a href="pdf.php?pdfile=File-name-here">Download File</a><br>
<a href="data/File-name-here.pdf">View File</a><br>

/* The following code assumes that the files are located in a folder
called "data" and all files are .pdf. I havent tested it with files that
have spaces in the name, so i dont know if it works. But it does work
fine with files using _ and - in the names. */

// Get the file size of the selected file (for convenience so you know how
// long you have to wait...
$len = filesize('data/'.$pdfile.'.pdf');

// Declare File Type...
header('Content-type: application/pdf');

// Name the file. Cound just call it download.pdf or other using this.
header('Content-Disposition: attachment; filename="'.$pdfile.'.pdf"');

// declare Files Size here (for the sake pf peoples sanity, please add this
header('Content-Length: '.$len);

// Path to original file to be read and sent to browser.

/* You could of course modify this to work with other filetypes, or just remove the filetype totally, its up to you.
Please make sure you leave in the file size bit so people dont go insane
waiting for a file of undetermined time, tearing their hair out...
13 Years
Discussion Span
Last Post by w3m

rather than download it, how do I display it in the browser.. which I know is what you are trying to avoid.. but it is what I am trying to achieve.. :P


for security reasons, don't forget to check if the filename is ok. I mean the $_GET in the pdf.php, I never use directly the variable, that user can possibly manipulate, hackers may try to get another important files that you might not want to deliver too.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.