Home  >  Article  >  Backend Development  >  How to Send the Correct PHP Headers for PDF File Downloads?

How to Send the Correct PHP Headers for PDF File Downloads?

DDD
DDDOriginal
2024-11-03 22:17:30151browse

How to Send the Correct PHP Headers for PDF File Downloads?

Sending Correct PHP Headers for PDF File Download

When attempting to evoke a PDF file download upon user interaction with a hyperlink, it is imperative to establish the appropriate headers. However, the solution presented in the initial inquiry appears to be ineffective.

To address this issue, consider the following corrected header configuration:

<code class="php">header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename=downloaded.pdf");
readfile("original.pdf");</code>

In this revised code snippet, the inline parameter in the Content-Disposition header has been replaced with attachment. This modification ensures that the PDF file is downloaded rather than being displayed inline within the browser.

Please note that it is crucial to ensure that the header() function is invoked before any output is generated. In PHP 4 and later versions, output buffering can be employed to circumvent this issue.

The above is the detailed content of How to Send the Correct PHP Headers for PDF File Downloads?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn