【发布时间】:2013-03-08 13:58:15
【问题描述】:
<?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 中的一个 sn-p 代码。我指的是使用的文件:
<a href="file.php?name=First File">File 1</a>
目的是点击链接,./curr/First File.pdf 应该下载。我确实得到了下载,但在检查时,它是在文件中嵌入了 pdf 的网页。有人可以帮忙吗?
【问题讨论】:
-
以上代码包含来自文件
file.php的所有内容? -
没有。下载周围有一个菜单系统等。以及登录验证。我应该丢失除 php 代码之外的所有内容吗?
-
@Splatter 如果页面上还有其他 HTML,下载后会输出。您的
file.php根本不应该有 ANY 标记。 -
@sirko 这行得通。你能把这个作为答案,让我给你打勾吗?