在PHP不工作的情况下向浏览器显示PDF

在PHP不工作的情况下向浏览器显示PDF,php,pdf,Php,Pdf,使用php调用浏览器显示给定PDF的功能在我的web主机上不起作用。但它可以在我的本地xamp服务器上使用apache PHP: $title = $_GET['title']; $loc = $_GET['loc']; header("Content-type: application/pdf"); header("Content-Disposition: inline; filename=".$title); @readfile($loc); 预期

使用php调用浏览器显示给定PDF的功能在我的web主机上不起作用。但它可以在我的本地xamp服务器上使用apache

PHP:

    $title = $_GET['title'];
    $loc = $_GET['loc'];

    header("Content-type: application/pdf");
    header("Content-Disposition: inline; filename=".$title);
    @readfile($loc);
预期输出:应该是正在呈现的PDF文档,就像在本地web服务器上一样

实际但不需要的输出: %PDF-1.6%���� 1638 0对象内部对象外部参照1638 44 00000000 16 00000N

可以在这里看到:

这是通过改变我手机上的某些东西来解决的吗?因为我的代码没有错。。。它在我的本地web服务器上工作。

使用此代码

<?php
$file = 'dummy.pdf';
$filename = 'dummy.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
@readfile($file);
?>

在写入标题之前,您需要确保没有发送任何文本

不应做的事情的示例:

<!DOCTYPE html>
<html>
...
<?php
header("Content-type: application/pdf");

...
...
此外,您的脚本非常不安全。下面是您应该做的,您的整个PHP脚本应该是:

<?php
$loc = filter_input(INPUT_GET,"loc");
$title = filter_input(INPUT_GET,'title')?:basename($loc);
if (!is_readable($loc)) {
    http_response_code(404);
    echo "Cannot find that file";
    die();
}
if (strtolower(pathInfo($loc,PATHINFO_EXTENSION)) != "pdf") {
   http_response_code(403);
    echo "You are not allowed access to that file";
    die();
}

header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=".$title);
header("Content-Length: ".filesize($loc));
readfile($loc);

我的头衔

删除@before readfile,这样您就可以获得更多错误的详细信息……我访问了该链接,响应标题包含
内容类型:text/html;charset=UTF-8
所以一定有什么东西在覆盖它们…@Masiorama删除了这一点,它只是一个空白screen@NeilD查看apokryfos注释,它将使您走上正确的道路。@apokryfos如何阻止它覆盖?你能告诉我这是什么原因吗。Cheeshi Palani,我的代码与此相同,但是,正如您在这个链接上看到的,它仍然以字母和符号显示。
<?php
$loc = filter_input(INPUT_GET,"loc");
$title = filter_input(INPUT_GET,'title')?:basename($loc);
if (!is_readable($loc)) {
    http_response_code(404);
    echo "Cannot find that file";
    die();
}
if (strtolower(pathInfo($loc,PATHINFO_EXTENSION)) != "pdf") {
   http_response_code(403);
    echo "You are not allowed access to that file";
    die();
}

header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=".$title);
header("Content-Length: ".filesize($loc));
readfile($loc);
<?php 
$loc = filter_input(INPUT_GET,"loc");
$title = filter_input(INPUT_GET,'title')?:basename($loc);
?>
<html><head><title>My title</title></head>
<body>
<iframe src="/view.php?<?php echo ($loc?"loc=$loc":"").($title?"title=$title":"") ?>">
</iframe>
</body>
<html>