Jump to content

How would I force a download of a PDF file


simcoweb

Recommended Posts

Thanks, Tarun, but I don't need a complete download system. It's one file in a protected directory. :)

corbin, I see this header reference that looks like the one:

header('Content-type: application/pdf');

Which, according to the manual text, forces the download.
I'd use:

[code]<?php
//$filename holds the filename, including the extension

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
@readfile($filename);
exit();

?>[/code]

Orio.
If you dont want the file to accessible by http, you have to options (that I can think of):


1) Put it in the root folder (outside of public_html / www / httpdocs or whatever your html folder is called), then tell your script the file is there and it will transfer the data.

2) Put it in a folder which has a .htaccess that has this following line:
[code]Deny from all[/code]
This way, no one will be able to access your file directly (IE- via http), but your php scripts will be able to.


Orio.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.