简体   繁体   English

使用php强制下载pdf

[英]Using php to force download a pdf

Im trying to get a website to have a button that forces a download of a pdf. 我试图让一个网站有一个强制下载pdf的按钮。

Heres the html of the button: 下面是按钮的html:

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

And the php script so far: 到目前为止php脚本:

    <?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)." “Adobe Reader无法打开'documents_ECM_IT_ResumeDownload.pdf',因为它不是受支持的文件类型,或者因为文件已损坏(例如,它是作为电子邮件附件发送的,并且未正确解码)。”

Any help would be greatly appreciated. 任何帮助将不胜感激。

EDIT Opened the pdf in a text editor and got this message: 编辑在文本编辑器中打开pdf并收到以下消息:

"
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 警告 :readfile(documents / ECM_IT_ResumeDownload.pdf)[function.readfile]:无法打开流:第4行的html / scripts / download.php中没有这样的文件或目录
"

The document is definitely there though. 该文件肯定在那里。 in html/documents/ECM_IT_ResumeDownload.pdf 在html / documents / ECM_IT_ResumeDownload.pdf中

$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);

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');

Have you tried getting rid of the closing PHP tag (the ?>) at the end? 你有没有尝试在最后摆脱关闭的PHP标签(?>)? 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. 它将页面视为纯PHP页面,删除可能意外附加到输出末尾的任何可能的新行。 This helped me when I was dynamically creating excel files for download, and they were downloading as corrupted. 当我动态创建excel文件以供下载时,这帮助了我,并且它们已经下载为已损坏。 Check out this page for more information: 查看此页面以获取更多信息:

http://www.php.net/manual/en/language.basic-syntax.phptags.php 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. 从您编辑的问题,似乎PHP无法找到该文件。 Try using an absolute path to the file like so: "c:\\blah\\de\\blah\\bloo.pdf" or "c:/blah/de/blah/bloo.pdf". 尝试使用文件的绝对路径,如:“c:\\ blah \\ de \\ blah \\ bloo.pdf”或“c:/blah/de/blah/bloo.pdf”。 If one of those paths works and downloads correctly, your relative path is incorrect in some way. 如果其中一条路径正常工作和下载,则您的相对路径在某种程度上是不正确的。

我总是使用Gowon Patterson的下载脚本,它还有热链接保护: http//by.gowondesigns.com/getfile/

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 . 您的下载脚本位于scripts/download.php ,您要scripts/download.php的文件位于documents/[...].pdf

Therefore, your readfile() function should be traversing to the parent directory (outside of scripts/ ), eg readfile('../documents/[...].pdf'); 因此, readfile()函数应该遍历父目录(在scripts/之外),例如readfile('../documents/[...].pdf'); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM