PDFファイルのダウンロードを強制する問題
-
23-10-2019 - |
質問
ファイルを強制的にダウンロードするための次の関数があります。
static public function download($file, $options=array()) {
$content = (isset($options['content'])) ? $options['content'] : '';
$contentType = (isset($options['contentType'])) ? $options['contentType'] : '';
header('Cache-Control: public');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.File::filename($file));
header('Content-Type: '.$contentType);
header('Content-Transfer-Encoding: binary');
if ($content!='') {
echo $content;
} else {
readfile($file);
}
}
PDFファイルとContentType = "Application/PDF"を送信します。問題は、ダウンロードしたPDFファイルを開こうとすると、「このドキュメントを開くエラーがありました。ファイルが破損している可能性がある」と書かれています。元のファイルを開くことができ、まったく同じように見えるので、奇妙です(ファイル名、サイズなど)
解決
この関数が実行される前に出力がないことを確認し、適切な尺度では、 exit
この関数の終わりに構築します:)
所属していません StackOverflow