0

我在 php 文档中使用以下代码来强制下载 pdf 表单,因为只有在本地机器上而不是在线提交后才能提交。

它可以正常下载文件,但会损坏它。我无法再打开 pdf 文档。

<?php 
$file_name = 'costumer.pdf';
$file_url = 'http://www.lopezi.com/forms/' . $file_name;
header('Content-Type: application/pdf');
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"".$file_name."\""); 
readfile($file_url);

?>
4

1 回答 1

1

The Content-Transfer-Encoding header shouldn't be needed in this case. Further I suspect that you have corruption in the outputted file.

Download it somewhere, open notepad, and drag the file in there. If any PHP warnings or errors were generated you will see them at the top.

Also, try to avoid the option of having more content return from the script, causing problems with the download, end with something like:

die(file_get_contents($file_url));

This way you cannot accidentally break the code easily by adding more output.

于 2013-04-16T16:29:34.260 回答