用PHP强制PDF下载 - 在Windows上文件损坏 [英] Force PDF download with PHP - file corrupt on Windows
问题描述
我有PHP代码强制下载PDF。它适用于Mac,但不适用于Windows机器。我猜这可能与linux服务器读取代码并创建一个mac可以读取但不是windows的文件有关?
$ filename = str_replace('','%20',$ _GET ['brochure']);
header('Cache-Control:public');
header('Content-type:application / pdf');
header(Content-disposition:attachment; filename = \$ filename \);
readfile('http://siteurl.com/media/download/'.$filename);
die();
有关如何获得此PDF下载的Windows友好的建议?
错误消息是
无法打开'filename ..',因为它不是受支持的文件
类型或文件已被破坏(例如,它是作为
电子邮件附件发送的,未正确解码)。
也许您的浏览器正在缓存较旧版本的文件。尝试将这些标题添加到代码中:
header(Cache-Control:maxage = 1);
header(Pragma:public);
另一个选项:
保存文件在临时目录中,并使用以下代码将其发送到客户端:
$ file = readfile('/ tmp /' 。$文件名);
header('Content-Description:File Transfer');
header('Content-Type:application / pdf');
header('Content-Disposition:attachment; filename ='。basename($ file));
header('Content-Transfer-Encoding:binary');
header('Expires:0');
header('Cache-Control:must-revalidate');
header('Pragma:public');
header('Content-Length:'。filesize($ file));
ob_clean();
flush();
readfile($ file);
出口;
I have PHP code forcing the download of a PDF. It works on a Mac but not from a Windows machine. I guess it's maybe something to do with the linux server reading the code and creating the file that a mac can read but not windows?
$filename = str_replace(' ', '%20', $_GET['brochure']);
header('Cache-Control: public');
header('Content-type: application/pdf');
header("Content-disposition: attachment; filename=\"$filename\"");
readfile('http://siteurl.com/media/download/'.$filename);
die();
Any suggestions on how to get this PDF download Windows friendly?
The error message is
Could not open 'filename..' 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).
Maybe your browser is caching an older version of the file. Try adding these headers to your code:
header("Cache-Control: maxage=1");
header("Pragma: public");
Another option:
Save the file in a temporary directory and use the following code to send it to the client:
$file = readfile('/tmp/'.$filename);
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
这篇关于用PHP强制PDF下载 - 在Windows上文件损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!