Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP文件下载,包括仅需要pdf时的.PHP文件_Php_Pdf_Readfile - Fatal编程技术网

PHP文件下载,包括仅需要pdf时的.PHP文件

PHP文件下载,包括仅需要pdf时的.PHP文件,php,pdf,readfile,Php,Pdf,Readfile,这是file.php中的一段代码。我使用以下方法引用该文件: <?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

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

<?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

?>


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

尝试将内容类型更改为:

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

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


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

上述代码包含文件
file.php
中的所有内容。否。下载周围有一个菜单系统等。以及登录验证。我是否应该丢失除php代码之外的所有内容?@Splatter如果页面上有其他HTML,则将使用downlaod获得输出。您的
文件.php
不应该有任何标记。@sirko这很有效。你能把这个作为答案,这样我就可以给你打勾了吗?我也得到了同样的答案。包含php的.php文件消失了。这个php的位置是%PDF-1.6%–ãÓ557 0 obj endobj 567 0 obj等等。我非常讨厌这个。作为一个用户,当我点击PDF链接时,我希望它在我的浏览器中打开。当网站强迫我下载可以在浏览器中查看的内容时,我绝对鄙视。如果我想的话,我可以下载它-完全脱离主题,我知道。我很感激。但该页面也有一个部分可以查看网站上的pdf(我承认,它是作为一个对象嵌入的)。这就是下载部分。
header("Content-Type: application/force-download");