Craig Traynor
Craig Traynor

Reputation: 449

Using php to force download a pdf

Im trying to get a website to have a button that forces a download of a pdf.

Heres the html of the button:

    <a href=scripts/download.php>
    <input type="image" src="images/download.gif" alt="Submit button"/>
    </a>

And the php script so far:

    <?php
    header('Content-Type: application/pdf');
    header('Content-disposition: attachment;filename=documents/ECM_IT_ResumeDownload.pdf');
    readfile('documents/ECM_IT_ResumeDownload.pdf');
    ?>

This seems to download the file fine but when I go to open it i get this error:

"Adobe Reader could not open 'documents_ECM_IT_ResumeDownload.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded)."

Any help would be greatly appreciated.

EDIT Opened the pdf in a text editor and got this message:

"
Warning: readfile(documents/ECM_IT_ResumeDownload.pdf) [function.readfile]: failed to open stream: No such file or directory in html/scripts/download.php on line 4
"

The document is definitely there though. in html/documents/ECM_IT_ResumeDownload.pdf

Upvotes: 11

Views: 64703

Answers (6)

FluffyKitten
FluffyKitten

Reputation: 14312

I always use Gowon Patterson's download script, it also has hotlink protection: https://github.com/gowonltd/getter (working link as at 2023)

Upvotes: 0

Jsoul
Jsoul

Reputation: 1

You can add : ob_clean(); just before the first header. This is to avoid blank spaces at the beginning of the file.

Upvotes: 0

Mohammad Intsar
Mohammad Intsar

Reputation: 463

$file_url = www.example.com/pdffolder/$pdfname;
header('Content-Type: application/pdf');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=".$pdfname);
readfile($file_url);

Upvotes: 29

M Miller
M Miller

Reputation: 5642

By the way, a bit late, but to identify the problem properly here:

Your download script is at scripts/download.php and the file you want to download is at documents/[...].pdf.

Therefore, your readfile() function should be traversing to the parent directory (outside of scripts/), e.g. readfile('../documents/[...].pdf');.

Upvotes: 0

abelito
abelito

Reputation: 1104

Have you tried getting rid of the closing PHP tag (the ?>) at the end? It will treat the page as a pure PHP page, removing any possible new lines that might accidentally get appended to the end of the output. This helped me when I was dynamically creating excel files for download, and they were downloading as corrupted. Check out this page for more information:

http://www.php.net/manual/en/language.basic-syntax.phptags.php

From your edited question, it seems like PHP is unable to find the file. Try using an absolute path to the file like so: "c:\blah\de\blah\bloo.pdf" or "c:/blah/de/blah/bloo.pdf". If one of those paths works and downloads correctly, your relative path is incorrect in some way.

Upvotes: 2

John Conde
John Conde

Reputation: 219794

Try removing the path to the file and just leave the file name in the content:

header('Content-Type: application/pdf');
header('Content-disposition: attachment; filename=ECM_IT_ResumeDownload.pdf');

Upvotes: 5

Related Questions