PHP PDF force download

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
skarphace
Forum Newbie
Posts: 3
Joined: Tue Aug 24, 2004 12:12 pm

PHP PDF force download

Post by skarphace »

I'm having problems with downloading served PDFs with IE. With Firefox everything works beutifully. The PDFs are in a non-public directory which is accessable to PHP. I have tried all kinds of different headers and googled until my eyes burned but could not find an answer to this. This is the code I'm using:

Code: Select all

$down = $_GETї'd'];
	
	$file = "/home/et/down" . $down;
	$doc_name = eregi_replace("ї/]+їA-Za-z]+ї/]", "", $down);
	#echo "docname: " . $doc_name;

	if (!$fsize=@filesize($file)) {
		die("There has been an error retrieving this file.");
	}

	header("Accept-Ranges: Bytes");
	header("Pragma: public");
	header("Expires: 0");
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Cache-Control: no-store, no-cache, must-revalidate");

	header("Content-type: application/force-download");
	header("Content-Disposition: attachment; filename=" . $doc_name);
	header("Content-Description: File Transfer");
	header("Content-Transfer-Encoding: binary");
	header("Content-Length: " . $fsize);

	$fp = fopen($file, 'r');
	$pdf_buffer = fread($fp, $fsize);
	fclose ($fp);

	print $pdf_buffer;

	exit();
I've tried stripping all but the necessary headers, rearranging them to suit IE but nothing. The purpose of this script is to make sure the user is authenticated and known before giving them a file(that portion has been left out). These pdfs can not go into a public directory. Any help would be very appreciated.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

header("Content-type: application/pdf");
....
readfile($file);
you don't really need the fopen stuff, unless you have an old php version.. (nor the print, since [php_man]readfile[/php_man] does all that)
skarphace
Forum Newbie
Posts: 3
Joined: Tue Aug 24, 2004 12:12 pm

Post by skarphace »

Yep, I know and I'll probably be going to that after this problem is solved but it is not causing this problem as I have been trying both ways.
skarphace
Forum Newbie
Posts: 3
Joined: Tue Aug 24, 2004 12:12 pm

To bump this thread up for the last time...

Post by skarphace »

This is an urgent issue for my project, if anyone has any suggestions, it would be appreciated.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

If you want to force a download, try:

Code: Select all

header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=$file");
This will force the browser to download the file because it doesn't know what type it is.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply