我想在用戶成功提交表單後下載PDF文件。PHP:試圖使pdf文件可下載
我已經使用了this question的代碼,但是pdf文件的內容被輸出爲gebrish字符而不是彈出的下載對話框。
下載代碼從功能
function phpfmg_thankyou(){
phpfmg_redirect_js();
//include('get_file.php');
$pdf_file = "{$_SERVER['DOCUMENT_ROOT']}/secured_assets/CRE_White_Paper_Release_01-15-2013.pdf";
if(file_exists($pdf_file)){
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=" . Urlencode('CRE_White_Paper_Release_01-15-2013.pdf'));
header("Content-Type: application/force-download");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Length: " . Filesize($pdf_file));
flush(); // this doesn't really matter.
$fp = fopen($pdf_file, "r");
while (!feof($fp)){
echo fread($fp, 65536);
flush(); // this is essential for large downloads
}
fclose($fp);
}
?>
<!-- [Your confirmation message goes here] -->
<br>
<div style="padding: 1em; background: #CDD7B6;">
<b>Your inquiry has been received. Thank you!</b>
<p><a title="FREE White Paper Commercial Real Estate Expectations" href="secured_assets/CRE_White_Paper_Release_01-15-2013.pdf">Click Here</a> to get your FREE copy of White Paper Commercial Real Estate Expectations</p>
</div>
<?php
} // end of function phpfmg_thankyou()
顯示您在此處使用的代碼。 – Cfreak 2013-03-22 19:12:25
看起來像忘記發送/不正確地發送內容類型標題。例如,'header(「Content-Type:application/octet-stream」);'你能向我們展示你的代碼嗎?特別重要的是,你要向我們展示文件頂部和「header(...)」調用之間的每一行(甚至是空白)。 – jedwards 2013-03-22 19:13:56
我認爲你的頭文件不能被髮送,因爲已經輸出了...我也看到PDF後的HTML嗎? – Wrikken 2013-03-22 19:27:01