php 使用php强制下载pdf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10576861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:30:13  来源:igfitidea点击:

Using php to force download a pdf

phphtmldownloadhttp-headers

提问by Craig Traynor

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.

任何帮助将不胜感激。

EDITOpened 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.phpon 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

采纳答案by abelito

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:

您是否尝试在最后去掉 PHP 结束标记(?>)?它会将页面视为纯 PHP 页面,删除任何可能意外附加到输出末尾的新行。当我动态创建要下载的 excel 文件时,这对我有所帮助,并且它们下载时已损坏。查看此页面了解更多信息:

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

从您编辑的问题来看,PHP 似乎无法找到该文件。尝试使用文件的绝对路径,如下所示:“c:\blah\de\blah\bloo.pdf”或“c:/blah/de/blah/bloo.pdf”。如果这些路径之一工作并正确下载,则您的相对路径在某种程度上是不正确的。

回答by Mohammad Intsar

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

回答by John Conde

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

回答by FluffyKitten

I always use Gowon Patterson's download script, it also has hotlink protection: http://by.gowondesigns.com/getfile/

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

回答by M Miller

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

顺便说一句,有点晚了,但要在这里正确识别问题:

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

您的下载脚本位于scripts/download.php,您要下载的文件位于documents/[...].pdf

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

因此,您的readfile()函数应该遍历父目录(在 之外scripts/),例如readfile('../documents/[...].pdf');.