PHPを使用してPDF/DOCファイルをダウンロードしています
これは私のhtmlコードです:
<a title="Download" target="_new" href="includes/pdf_server.php?file=test.pdf">Test PDF</a>
これは pdf_server.php ファイルの私の php コードです
<?php
$file = $_GET["file"];
if (file_exists("../PDF/".$file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename=' . urlencode(basename($file)));
// header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize("../PDF/".$file));
ob_clean();
flush();
readfile($file);
exit;
}
?>
PDFは、test.pdfファイルがあるフォルダーです。リンクをクリックしてファイルをダウンロードすると。ブラウザにはダウンロードするファイルがサイズ (1.4 MB) で表示されますが、ダウンロードが完了してファイルを開くと、ファイルが破損しているか、サポートされていないファイルのいずれかのエラーが表示されます。次に、0バイトを示すプロパティを確認します。
助けてください