0
<?php

$file = $_GET['name'];

$path = './curr/'.$file.'.pdf'; // the file made available for download via this PHP file
$mm_type="application/pdf"; // modify accordingly to the file type of $path, but in most cases no need to do so

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($path)) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");

readfile($path); // outputs the content of the file

?>

这是file.php 中的一段代码。我指的是使用以下文件的文件:

<a href="file.php?name=First File">File 1</a>

目的是点击链接,./curr/First File.pdf应该下载。我确实得到了下载,但在检查时,它是在文件中嵌入了 pdf 的网页。有人可以帮忙吗?

4

2 回答 2

1

如果您只想加载 PDF,上面的代码就是要执行的所有代码。

删除所有周围的菜单、页眉或页脚。确保readfile()在调用此链接时,除了来自 PDF 的任何 HTML 或任何其他输出。

于 2013-03-08T14:14:22.627 回答
0

尝试将内容类型更改为:

header("Content-Type: application/force-download");
于 2013-03-08T14:00:37.220 回答