Page 1 of 1

PHP PDF force download

Posted: Tue Aug 24, 2004 12:12 pm
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.

Posted: Tue Aug 24, 2004 12:30 pm
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)

Posted: Tue Aug 24, 2004 2:01 pm
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.

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

Posted: Thu Aug 26, 2004 3:33 pm
by skarphace
This is an urgent issue for my project, if anyone has any suggestions, it would be appreciated.

Posted: Thu Aug 26, 2004 4:04 pm
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.