Force file download

Hi there,

i’m trying to use headers to force a file to be downloaded from a site.

Here’s what I have:


<?php

//mysql queries and handling here

...

//set headers
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename='catalogue.pdf'");
header("Content-Type: application/pdf");
header("Content-Transfer-Encoding: binary");

//read the file from disk
readfile('http://' . $_SERVER['SERVER_NAME'] . '/images/uploads/catalogue 2011.pdf');

//include file here containing HTML tags & page construction

?>

When I load the page, the pdf downloads immediately, rather than having to click on a link, and I cannot open it. Any program that tries to open it complains that it’s a corrupt file.

I’m also getting a number of warnings saying I cannot modify header information, it has already been set in ‘header.html.php’. Well that doesn’t make sense cos ‘header.html.php’ is included AFTER I have set the headers.

Any help would be great.

Many thanks,
Mike

Just an update for those who got readfile() function disabled on their host, try this code instead:


$handle = fopen($filepath, 'rb');
$buffer = '';
$chunksize = 1 * (1024 * 1024);

while (!feof($handle)) {
	$buffer = fread($handle, $chunksize);
	echo $buffer;
	ob_flush();
	flush();
}

fclose($handle);

this will read the file using fopen() function and will split it into chunks not to allocate too much system memory. Note that $filepath is a real path to the file.

It would appear that you are trying to bring down a pdf file followed by an html page. Not going to work. You can do one or the other but not both.

Delete everything after the readfile() call and it should then work.

Thank you both for your input.

My question then is how do I then load my page, all the HTML is in the include file. If I delete everything after the readfile() command I’ll download the file but not display the page.

If instead of using a link to the pdf file like so:


<a href='catalogue.pdf'>Click here to download this file</a>

I do this:


<a href='download_script.php'>Click here to download this file</a>

Then presumably a new page will load that will download the file.

Then I guess I put a final header:


header('Location: http://localhost');

to take me back to the page where the link was at.

I get the feeling that that wouldn’t work, but if not I’m not sure what would…

Again, many thanks for your help,

Mike

When you force a file to download the page with the link on it that was used to download it stays displayed in the browser so simply delete your extra header(‘Location: http://localhost’); header as there is no need for any code to take you back to the page that is still displayed in the browser after the file finishes downloading as it is still there anyway.

The following is all you need in the page to force the PDF to download and stay on the same page in the browser.

<a href='download_script.php'>Click here to download this file</a>

readfile('http://' . $_SERVER['SERVER_NAME'] . '/images/uploads/catalogue 2011.pdf');

Do not read a file from a URL…the server then needs to upload it twice. Do not make the server do that. ALWAYS use file paths.

Ok guys, thanks very much for your help, I’ll take another look.

Cheers,
Mike

WOO-HOO! (in the style of Homer Simpson)

Yes, that works a treat.

Thanks for your time and patience guys!

M