【问题标题】:PHP file download including the .php file when only pdf is wanted只需要 pdf 时的 PHP 文件下载,包括 .php 文件
【发布时间】: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 这行得通。你能把这个作为答案,让我给你打勾吗?

标签: php pdf readfile


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    尝试将内容类型更改为:

    header("Content-Type: application/force-download");
    

    【讨论】:

    • 我也一样。 php 消失的 .php 文件。此 php 所在的位置是 %PDF-1.6 %âãÏÓ 557 0 obj > endobj 567 0 obj 等。
    • 我非常讨厌这个。作为用户,当我单击 PDF 链接时,我希望它在我的浏览器中打开。当网站强迫我下载可以在我的浏览器中查看的东西时,我绝对鄙视。如果我愿意,我可以下载它——我知道,完全跑题了。
    • 我很感激。但是该页面也有一个部分可以查看网站上的 pdf(我承认,作为对象嵌入)。这是专门的下载部分。
    猜你喜欢
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    • 2017-08-04
    相关资源
    最近更新 更多