Jump to content

PHP Heaers / Force download


_tina_

Recommended Posts

Hi,

 

I have a page, you click on a link and are brought to a new page which forces a PDF download.

The problem is that the file starts to download and the page doesn't even load an stays on the current page.

So I click the link, the file starts but I stay on the current page.  Is there a way around this?

 

Thanks in advance.

 

This is the page that forces the download which never loads.

 


header("Content-type: application/pdf");
header("'Content-Disposition: attachment; filename=\"email.pdf\"");
readfile($file);

include("header.php");
//sleep(5);
?>
<br>
<p>
Page content..
</p>

Link to comment
Share on other sites

The problem is you're trying to serve two different types of content on one page. The headers you're sending to the web browser basically say "I'm sending you a PDF file, and I'm going to start outputting it now".

 

After this declaration, you've got HTML, which isn't what the browser is expecting (and rightly so, because you've told it otherwise). So the browser downloads the file, and behaves like it would if you were linking directly to a zip file or something. Try it, create a little HTML page with an anchor link to something that the browser can't display natively (so no images, audio/movie files etc.) and you'll see exactly the same behaviour: the browser starts the download, but stays on the page with the link.

Link to comment
Share on other sites

Thanks for that Alex, that makes a lot of sense.  I guess I'll have to find an alternate way to achieve this.  Thanks again.

 

The problem is you're trying to serve two different types of content on one page. The headers you're sending to the web browser basically say "I'm sending you a PDF file, and I'm going to start outputting it now".

 

After this declaration, you've got HTML, which isn't what the browser is expecting (and rightly so, because you've told it otherwise). So the browser downloads the file, and behaves like it would if you were linking directly to a zip file or something. Try it, create a little HTML page with an anchor link to something that the browser can't display natively (so no images, audio/movie files etc.) and you'll see exactly the same behaviour: the browser starts the download, but stays on the page with the link.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.