繁体   English   中英

强制下载未放在服务器上的文件

[英]Force download file that's not placed on the server

我正试图强制下载我正在生成的pdf文件。 我不需要将pdf文件实际保存在服务器上。

所以当我生成我的pdf文件时,我会得到文件内容。 然后我用base64编码它。 现在的问题是我需要强制下载它。 我已经浏览了整个网络,但是我没有找到任何搜索结果告诉我如何在没有实际放置在网站上的文件的情况下执行此操作。

我试过以下代码:

header("Content-type: application/pdf");
header("Content-disposition: attachment; filename=\"invoice.pdf\""); 
header("Content-Length: ".filesize($pdffile));
readfile(base64_decode($pdffile));

但是,它给了我一个损坏的pdf文件,(1 kb)。 实际文件大约应该是50kb。

关于我能尝试什么的任何想法?

readfile尝试从文件输出内容,但您只有数据字符串。 试试这个:

header("Content-type: application/pdf");
header("Content-disposition: attachment; filename=\"invoice.pdf\""); 
echo base64_decode($pdffile);

我还建议将$ pdffile重命名为$ pdfcontent,以便更好地澄清。

暂无
暂无

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

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