Home > Article > Backend Development > How to Send the Correct PHP Headers for PDF File Downloads?
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!